javascript - Promise.all 不等待新的 Promise 解决

标签 javascript node.js promise single-page-application

这是使用 nodejs 和 jquery 的水疗中心的一部分。 getToday 函数控制台记录“结果:”,因此整个过程应该控制台记录 1,然后记录结果:,然后记录 2,但它不会等待 getToday 完成。输出为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)
    })
})

为什么这不等待新的 Promise 解决?

最佳答案

如果 getToday 是异步的并返回一个 Promise,请将其添加到链中,而无需使用看起来无用的额外 Promise 进行包装:

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

关于javascript - Promise.all 不等待新的 Promise 解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53871762/

相关文章:

javascript - CoffeeScript 中的 Protractor 测试生成 "SyntaxError: unexpected by"?

javascript - 仅向使用 Chrome 18 的用户显示单个网页

testing - Ember.js Mocha 测试使用异步代码随机失败

c# - Asp.net Button 组件在 Twitter-Bootstrap 主题下不起作用

javascript - 如果存在带有逻辑运算符的 js 注入(inject),将渲染/发送到 ReactDOM.render() 作为参数

javascript - 在nodejs中提取表单数据

node.js - 使用 ECONNREFUSED 在本地连接到 DynamoDB 时出现问题

node.js - Github Action err : bash: line 3: npm: command not found

javascript - AngularJs - 在工厂内的多个函数之间共享数据

Angular 4 RxJs WebSocket