简体   繁体   English

使用 await/async 和 promise 等待代码完成

[英]Wait for code to complete using await/async and promise

I am trying to use await/async along with Promise.all to get some arrays and then move on with the code but when I simulate a delay in the code it appears to not be working.我正在尝试将 await/async 与 Promise.all 一起使用来获取一些 arrays,然后继续使用代码,但是当我模拟代码中的延迟时,它似乎不起作用。 My code is below我的代码如下

    async getAllTheUnits() {
        // get all units
        console.log('allunits called 1')
        let unitRef = db.collection('units')
        try {
            let allUnitsSnapShot = await unitRef.get()
            await Promise.all(allUnitsSnapShot.docs.map(async (doc) => {
                let u = doc.data()
                u.id = doc.id
                await this.allOfTheUnits.push(u)
            }))
            console.log('allunits called 2 ', this.allOfTheUnits.length)
        }
        catch (err) {
            console.log('Error getting all the units', err);
        }
    },
    async getAllTheWeeks() {
        // get all weeks
        console.log('allweeks called 1')
        let weekRef = db.collection('weeks')
        try {
            let allWeeksSnapShot = await weekRef.get()
            await Promise.all(allWeeksSnapShot.docs.map(async (doc) => {
                let w = doc.data()
                w.id = doc.id
                await this.allOfTheWeeks.push(w)
            }))
            console.log('allweeks called 2 ', this.allOfTheWeeks.length)
        }
        catch (err) {
            console.log('Error getting all the weeks', err);
        }
    },

Here is a simplified function where I call these other functions这是一个简化的 function 我称之为其他函数

    async initUsersCourse() {

        console.log('1')

        await this.getAllTheUnits()
        await this.getAllTheWeeks()

        console.log('2')

        const allUnits = this.allOfTheUnits
        const allWeeks = this.allOfTheWeeks

        console.log('3')
        console.log('allUnits ', Array.isArray(allUnits), allUnits.length   )
        console.log('allWeeks ', Array.isArray(allWeeks), allWeeks.length)
    }

As stated above.如上所述。 If I put in a settimeout in the getAllTheWeeks function to simulate a delay the initUsersCourse function does not wait and moves on to console.log('2')如果我在getAllTheWeeks function 中设置了一个settimeout来模拟延迟initUsersCourse function 不会等待并继续前进到console.log('2')

your function must return a promise, you need resolve the promise after the timeout finish:您的 function 必须返回 promise,您需要在超时完成后解决 promise:

async getAllTheWeeks() {
    return new Promise( resolve => setTimeout( resolve, 2000 ) );
}

for your specific code problem, Promise.all want an array of promise, so the function you give at your allWeeksSnapShot.docs.map must return a promise, that's not currently the case so promise.all must return directly for your specific code problem, Promise.all want an array of promise, so the function you give at your allWeeksSnapShot.docs.map must return a promise, that's not currently the case so promise.all must return directly

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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