我需要帮助ES6 Promises链接数组处理。
当解析中有其他异步方法时,如何处理/定义进入namespace Sample.AsyncQueries
{
public class PersonSearchCall : IQuery<PersonSearch>
{
public string Name { get; set; }
public int Id { get; set; }
}
public class PersonSearchCallHandler : IAsyncQueryHandler<PersonSearchCall, PersonSearch>
{
private readonly IQueryProcessor queryProcessor;
private readonly ICommandProcessor commandProcessor;
public PersonSearchCallHandler(ICommandProcessor commandProcessor,
IQueryProcessor queryProcessor)
{
this.queryProcessor = queryProcessor;
this.commandProcessor = commandProcessor;
}
public async Task<PersonSearch> HandleAsync(PersonSearchCall query)
{
var client = new PersonServiceSoapClient();
var personResponses = await client.PersonSearchAsync(inputs).ConfigureAwait(false);
//Build the person Object
return person;
}
}
}
方法的每个数组项?
以下是简化示例:
Promise.all
答案 0 :(得分:1)
你可以这样做,你可以在第一个获得sub-sub-categories
的承诺中添加sub-category
处理程序,然后当它可用时返回你想要的对象。然后,.then()
的已解析结果将是您想要的对象数组:
place
仅供参考,因为您正在转换为承诺,为什么不从Promise.all()
返回承诺而不在那里使用回调?您当前的代码无法回复function getData(data, callback) {
let groupPromises = data.map(row => {
var coordinates = getCoordinates(row);
// add .then() handler here to convert the place result
// into the object you want it in
return getPlaces(coordinates).then(place => {
return {place: place, data: row};
});
});
return Promise.all(groupPromises)
.then(groups => callback(groups))
.catch(err => {
console.log(err);
throw err;
});
}
}
function getPlaces(coordinates) {
return new Promise(function(resolve, reject) {
if(coordinates == null) {
reject();
}
parameters = {
location: [coordinates.latitude, coordinates.longitude],
rankby: "distance",
};
googlePlaces.searchPlace(parameters, function (error, response) {
if (error) {
reject(error);
};
resolve(response);
});
}
}
中的错误,这是一个很大程度上免费承诺的错误。
事实上,凭借纯粹的承诺,getData()
可以简化为:
getData()