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'))
プロセスなしに、すべての非同期要求"同期" の実行が
let requests = [1,2,3].map((item) => {
return new Promise((resolve) => {
asyncFunction(item, resolve);
});
})
Promise.all(requests).then(() => console.log('done'));
(2よりも速く1を終了してもよい)、私はそのようにそれをやった
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:のuserId \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、関数(コンタクト、コールバック){ \t \t \t \t \t \tにconsole.log(コンタクト) \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場合: \t \t \tをcontact.contactId \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