0
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.

Ayoub
  • 1,417
  • 14
  • 24
user3556152
  • 105
  • 8
  • What is `err[0]` in `promises` array supposed to be? Is it typo? – Paulooze Nov 12 '17 at 13:05
  • yup, sorry. that was supposed to be arr[0] – user3556152 Nov 12 '17 at 13:06
  • `arr[0].map` returns `'aaa'`, is it what you expect it to return? – Paulooze Nov 12 '17 at 13:11
  • it returns the promise created at `pollInstall` does it not? – user3556152 Nov 12 '17 at 13:14
  • Ok, let me rephrase it: `arr[0].map(id => this.installPackage(id))` returns `[this.installPackage('aaa')]`, nothing more. Are you sure `this` is what you think it is? I think `run()` doesn't have `installPackage` function in `this`, Try `arr[0].map(id => installPackage(id))`. – Paulooze Nov 12 '17 at 13:17
  • I have updated the code snippet to be a bit more clear. To answer the question, Yes `installPackage('aaa')` is part of `this`. sorry for the confusion. – user3556152 Nov 12 '17 at 13:24

2 Answers2

2

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);
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

your pollInstall function return a Promise which is not resolving, try this:

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