(PHPからの)Node.jsのコードを書くのはまだ新しく、特に複数のネストされたデータベース呼び出し/非同期操作がある場合、非同期操作が正しく機能するかどうかを理解することができません。 。node.jsのネストされた非同期操作
たとえば、このコード(mongoを使用)では、必要なアカウントにdeleted
が設定されている場合にのみプログラムが終了しますか? (todo
1、2および3参照):
// array of account emails to be compared against the accounts already in the database (using `email`)
var emailsArray;
accountsDatabase.find({}, {})
.then(function (accountsInDB) {
return q.all(_.map(accountsInDB, function (dbAccount) {
// compare account's email in db with emails array in memory using .email
if ((emailsArray.indexOf(dbAccount.email) > -1) === false) {
// todo 1. should there be another 'then' here or is 'return' ok in order for it to work asynchronously?
return accountsInDB.updateById(dbAccount._id, {$set: {deleted: true}});
} else {
// todo 2. is this return needed?
return;
}
}));
})
.then(function() {
// TODO 3. WILL THE PROGRAM POTENTIALLY REACH HERE BEFORE `deleted` HAS BEEN SET ON THE REQUIRED ACCOUNTS?
callback(); // all of the above has finished
})
.catch(function (err) {
callback(err); // failed
});
'callback'パラメータを取る代わりに、あなたが作成した約束を'返す 'だけです。 – Bergi