2016-11-16 2 views
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] ? 
    } 
}); 
+1

네, 반복기와 배열에 대한 순서가 정해져 있습니다. 객체 키의 순서가 유지되도록 시도됩니다. – megawac

답변

0

배열을 전달하면 항상 순서대로 처리됩니다. 그리고 공식 문서에 따르면 '지도에 객체가 전달되면 결과는 배열이됩니다. 결과는 개략적으로 원래 Objects '키의 순서대로 표시됩니다 (JavaScript 엔진에 따라 다를 수 있음). 의사가 배열에 대해 아무 말도하지 않아 주문이 보존된다고 가정하는 것이 안전합니다.