等待Promise.all后执行停止

时间:2017-11-12 12:59:58

标签: javascript node.js es6-promise

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

使用上面的代码片段 await Promise.all(promises)之后的任何内容都没有执行,并且据我所知,没有抛出任何错误。

有没有人知道为什么会出现这种情况?或者能够在问题的方向上刺激我。

2 个答案:

答案 0 :(得分:2)

避免Promise constructor antipattern并忘记解决承诺。相反,写

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

答案 1 :(得分:1)

你的server { listen 443 ssl; server_name lb.domain.com; ssl_certificate /var/www/ssl/domain.com.crt; ssl_certificate_key /var/www/ssl/domain.com.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { include /etc/nginx/mime.types; proxy_pass http://domain; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 返回一个无法解析的pollInstall function,试试这个:

Promise