如何在数组中链接promise

时间:2017-02-01 21:17:07

标签: node.js es6-promise

我需要帮助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

1 个答案:

答案 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()