我需要进行多个异步调用,并确保结果保持与调用相同的顺序。如何使用async
库实现该目标?
async.map(['item1', 'item2', 'item3'], function (itemName, callback){
// Ajax GET item on server
// ...
// Once we have the item, return it
callback(null, item);
}, function(error, results){
// The results array is not necessarily sorted by order of calls
// It can be [item2, item3, item1]
// I want to ensure it will always be [item1, item2, item3]
});
最佳答案
结果数组不一定按调用顺序排序
可以是[item2, item3, item1]
好吧,不。你为什么这么认为? The docs明确声明:
结果数组将与原始arr
相同。