我将一个数组传递给async.each()
,其中执行将通过if
条件,它在进入else
语句时满足条件时将项目添加到数组中,执行不会等待shippo响应。它移动到下一行并将响应发送到服务器。
我注意到我们在shippo
几秒钟后得到了响应,所以我在块setTimeout()
完成后放置了一个函数else
,但即使在我的控制台中,消息'c'
也会最后打印。我对回调非常困惑,它们将如何工作。有没有其他方法可以停止执行流程,直到我们得到响应shippo
。
var async = require('async');
import each from 'async/each';
async.each(trackingnumbers, function (value, callback) {
value['TrackingNo'] = parseInt(value['TrackingNo']);
value['Provider'] = value['Provider'].toLowerCase();
if (value['Provider'] == "domestic") {
items.push({ 'TrackingNo': value['TrackingNo'], 'Provider': value['Provider'], 'Status': 'Completed' });
console.log('a');
}
else {
console.log('b');
shippo.track.get_status(value['Provider'], value['TrackingNo']).then(function (status) {
console.log('c');
items.push({ 'TrackingNo': value['TrackingNo'], 'Provider': value['Provider'], 'Status': status.tracking_status });
}, functionconsole.log('d'); (err) {
console.log("There was an error retrieving tracking information: %s",+ err);
callback('There was an error retrieving tracking information:');
//
});
console.log('d');
e }
console.log('e');
setTimeout(function(){
callback();
},3000);
}, function (err) {
if (err) {
console.log('error occured ' + '|' + err);
}
else {
console.log(items);
res.setHeader("Access-Control-Allow-Origin", "*");
res.send(items).status('200');
}
});