简体   繁体   中英

How to use Async/Await to hold the execution? [on hold]

I am trying to fetch the user Id based on the validation of some parameters. The issue is that the code is not being executed in the right sequence !!

Output is

  1. KlFHdAgDUUUIKaPeObs9RiSBlOp1
  2. Notifications true
  3. Animals true

Output should be

  1. Notifications true
  2. Animals true
  3. KlFHdAgDUUUIKaPeObs9RiSBlOp1
exports.bulkMsg = functions.firestore
    .document('/bulkMsgs/{bulkMsgsId}').onUpdate(async(snap, context) => {

        function processBulKMsg(onUsers) {
            anPromises = [];
            notPromises = [];

            onUsers.forEach((oneUser) => {
                var status = false;
                if (oneUser.id === 'KlFHdAgDUUUIKaPeObs9RiSBlOp1') {


                    const onNot = admin.firestore().collection('users').doc(oneUser.id).collection('notifications').get().then(oneNot => {
                        if (notNumStatus === true) {
                            if ((oneNot.size >= notNumMin) && (oneNot.size <= notNumMax)) {
                                status = status ? true : false;
                            } else {
                                status = false;
                            }
                        }
                        console.log('Notifications', status);
                        return oneNot;
                    }).catch(err => {
                        console.log('Error getting notifications', err);
                        return null;
                    });

                    notPromises.push(onNot);


                    const allAn = admin.firestore().collection('users').doc(oneUser.id).collection('animals').get().then(oneAn => {
                        if (anCountStatus === true) {
                            if ((oneAn.size >= inFarmMin) && (oneAn.size <= inFarmMax)) {
                                status = status ? true : false;
                            } else {
                                status = false;
                            }
                        }
                        console.log('Animals', status);
                        return oneAn;
                    }).catch(err => {
                        console.log('Error getting animals', err);
                        return null;
                    });

                    anPromises.push(allAn);

                    if (status) {
                        console.log(oneUser.id);
                    }

                }
            });
            return Promise.all([anPromises, notPromises]);
        }

        return admin.firestore().collection('users/')
            .where("notification", "==", "true")
            .get().then(onUsers => {
                return processBulKMsg(onUsers);
            }).catch(err => {
                console.log('Error getting user', err);
                return null;
            });

    });

The code is working as expected. Data is loaded from Firestore asynchronously, and while that data is being loaded, your main code path continues to execute.

This code is in the main code path of your processBulKMsg function:

if (status) {
    console.log(oneUser.id);
}

So this console.log prints pretty much immediately when you call processBulKMsg . If you want to log the user ID later, you should only call it after the promises have resolved. Something like this

return admin.firestore().collection('users/')
    .where("notification", "==", "true")
    .get().then(onUsers => {
        let result = processBulKMsg(onUsers);
        console.log(oneUser.id);
        return result;
    })...

Alternatively, you can add the oneUser.id to the array of promises, and use it from there.

So in processBulKMsg , you'd do:

...
return Promise.all([anPromises, notPromises, oneUser.id]);

And then

return admin.firestore().collection('users/')
    .where("notification", "==", "true")
    .get().then(onUsers => {
        let result = processBulKMsg(onUsers);
        console.log(result[result.length -1]);
        return result;
    })...

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