简体   繁体   English

Promise.all不等待新的Promise解决

[英]Promise.all doesn't wait for new Promise to resolve

This is part of a spa that uses nodejs and jquery. 这是使用nodejs和jquery的spa的一部分。 The getToday function console logs "result:" so the whole thing should console log 1, then result:, then 2 but it does not wait for getToday to complete. getToday功能控制台记录的是“结果:”,因此整个过程应该记录控制台日志1,然后是结果:,然后是2,但是它并不等待getToday完成。 The output is 1, 2, result. 输出为1、2,结果。

app.get('/incidents/all', function(req, res){
    let data = globals.dataTopdesk;
    let output = {}
    Promise.all([
        data.length,
        calculateProcessingStatus(data),
        calculateOnHold(data),
        calculateOpen(data),
        calculateStatus(data),
        calculateCallType(data);
    ])
    .then(function(values) {
        output.all = values[0]
        output.processingStatus = values[1],
        output.onHold = values[2],
        output.open = values[3],
        output.status = values[4],
        output.callType = values[5]
        return output;
    })
    .then(function(output) {
        console.log('1')
        return new Promise(resolve=> {
            output.today = getToday(db, output)
            resolve(output);
        })
    })
    .then(function(output) {
        console.log('2')
        res.send(output)
    })
    .catch (function(err) {
        res.send('failed: ' + err)
    })
})

Why is this not waiting for the new Promise to resolve? 为什么这不等待新的Promise解决?

If getToday is async and returns itself a promise, add it inside the chain without wrapping with an extra promise which looks useless: 如果getToday是异步的并返回一个promise,则将其添加到链中,而不用多余的promise包裹,这看起来似乎没有用:

  return getToday().then(data => { output.today = data; return output; }); 

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

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