제 3 자 API 호출하고 모든 반환 수집하고 내 API에서 1 배열로 반환하는 데 문제가 있습니다. 내가 성공적으로 전화를 걸고 돌아 오는 것을 알 수 있습니다. asynch 때문에 최종 배열은 채워지기 전에 반환됩니다. 이것을 처리 할 수있는 우아한 솔루션이 있습니까?배열에서 API 반환 수집
var itemIds = ['1','2','3','4','5','6']
exports.getItemData = function getItemData(req, res) {
var items = [];
var errors = [];
for(var itemId in itemIds) {
var options = {
uri: itemEndpoint + itemIds[itemId] +'/',
json: true
};
RequestPromise(options).then(function (item){
console.log(item);
items.push(item);
}).catch(function(err){
console.log(err)
errors.push(err);
});
};
res.type('application/json');
res.json(items);
};
['Promise.all'] (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all). –