使用ES6諾言 (一個承諾庫可用於舊的瀏覽器):
過程保證同步執行(例如,1,然後2然後3)
function asyncFunction (item, cb) {
setTimeout(() => {
console.log('done with', item);
cb();
}, 100);
}
let requests = [1, 2, 3].reduce((promiseChain, item) => {
return promiseChain.then(new Promise((resolve) => {
asyncFunction(item, resolve);
}));
}, Promise.resolve());
requests.then(() => console.log('done'))
處理所有異步請求,而不所有請求「同步」 執行(2可以更快地完成比1)
let requests = [1,2,3].map((item) => {
return new Promise((resolve) => {
asyncFunction(item, resolve);
});
})
Promise.all(requests).then(() => console.log('done'));
我做到了對這樣
Promise.all(body.schedules.map(function(scheduleId) {
return new Promise(function(resolve, reject) {
return scheduleSchema.findOneAndRemove({
_id: scheduleId
})
.then(function() {
logSchema.insert({
userId: req.user.id,
actId: constants.LOG_SCHEDULE_DELETE.id,
extData: scheduleId
});
resolve();
})
.catch(function(err) {
reject(err);
});
});
})).then(function() {
return res.json({
code: constants.SUCCESS_CODE
});
}).catch(function(err) {
return res.json(constants.DATABASE_ERROR);
});
最後一個例子
function callback (result) { console.log('all done'); }
[1, 2, 3].forEach((item, index, array) => {
asyncFunction(item,() => {
if (index === array.length - 1) {
callback();
}
});
});
這並不能保證所有的項目都處理後回調將執行。它只保證在處理最後一個項目後執行回調。
More information
邁克爾。
使用async:'user_collection的工作版本。findOne({ \t \t \t \t _id:用戶id \t \t \t},功能(錯誤,用戶){ \t \t \t \t如果(誤差) \t \t \t \t \t回調(誤差) \t \t \t \t否則{ \t \t \t \t \t async.forEach(user.contac ts,function(contact,callback){ \t \t \t \t \t \t console.log(contact); \t \t \t \t \t \t如果(contact.accepted == '真'){ \t \t \t \t \t \t \t user_collection.findOne({ \t \t \t \t \t \t \t \t _id:contact.contactId \t \t \t \t \t \t \t},fu nction(誤差,接觸){ \t \t \t \t \t \t \t \t result.push(接觸); \t \t \t \t \t \t \t \t callback(); \t \t \t \t \t \t \t}) \t \t \t \t \t \t} \t \t \t \t \t},函數(誤差){回調(錯誤,結果)}) \t \t \t \t} \t \t \t} );' – johnny