Search code examples
javascriptnode.jses6-promise

Execution stops after await Promise.all


const tooling = require("tooling")
module.export = {
    run : async function(){
        let arr = [["aaa"],["bbb", "ccc"]]
        let promises = arr[0].map(id => this.installPackage(id))
        await Promise.all(promises)
        console.log("Finished Installing")
    }

    installPackage : async function(id){
        let requestId = await tooling.create(id)
        return this.pollInstall(requestId)
    }

    pollInstall : function(id){
        return new Promise((resolve, reject) => {
            tooling.retrieve(id).then(resp => {
                if(resp.Status === "SUCCESS") return resp.Status
                else setTimeout(() => {this.pollInstall(id)}, 5000)
            })
        })
    }
}

With the above code snippet anything after await Promise.all(promises) dose not execute and no errors are being thrown as far as I can tell.

Does anyone have any insight into why this might be the case? or would be able to prod me in the direction of the issue.


Solution

  • Avoid the Promise constructor antipattern and forgetting to resolve the promise. Instead, write

    pollInstall: async function(id) {
        const resp = await tooling.retrieve(id);
        if (resp.Status === "SUCCESS") {
            return resp.Status;
        } else {
            await new Promise(resolve => { setTimeout(resolve, 5000); });
            return this.pollInstall(id);
        }
    }