0
如果我對async.map
進行了一系列調用,結果是否保證了順序?或者結果只是請求完成的順序?例如async.map保證結果是按順序傳遞的嗎?
var requestList = [
{ method: 'GET', url: '/something1' },
{ method: 'GET', url: '/something2' },
{ method: 'GET', url: '/something3' }];
// async map the requests together, and handle the result in
// the callback.
async.map(requestList, function(obj, callback) {
request(obj, function(error, response, body) {
if(!error && response.statusCode == 200) {
var body = JSON.parse(body);
callback(null, body);
} else {
callback(error || response.statusCode);
}
});
}, function(err, result) {
if(err) {
console.log('Error!');
} else {
// is result guarenteed to be in the order: [/something1, /something2, /something3] ?
}
});
是的,順序是迭代器和數組的保證。嘗試保持對象鍵的順序 – megawac