简体   繁体   中英

Nested async operations in node.js

I am still quite new to writing code for Node.js (coming from PHP) and sometimes struggle to understand if async operations are working correctly, especially when there are multiple nested database calls/async operations.

For example, in this piece of code (which uses mongo), will the program only finish when deleted has been set on any required accounts? (see todo 1, 2 and 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
    });

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}}); 

You can chain another then here if you want/need, but that's not important. What is important is that you return the promise from the function, so that it can be awaited. If you didn't, the function would still work asynchronous, but out of order - it would just continue immediately after having started the update operation.

is this return needed?

 else return; 

Nope. It just returns undefined , like no return would have done as well. You can omit the whole else branch.

WILL THE PROGRAM POTENTIALLY REACH HERE BEFORE deleted HAS BEEN SET ON THE REQUIRED ACCOUNTS?

 callback(); // all of the above has finished 

No, it won't. The map produces an array of promises, and q.all makes a promise that waits for all of them (and fulfills with an array of their results). The then will wait for this promise that is returned from its callback before the chain proceeds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM