javascript nodejs promise 所有内容稍后执行

标签 javascript node.js promise

我有多个这样的 promise :

module.exports = function (a, b, c) {
    return someAsync(() => {
            someFunc(a);
        })
        .then(() => myPromises(a, b, c))
        .then(result => {
            console.log('log the result: ', JSON.stringify(result)); // empty
        })
};


const myPromises = function myPromises(a, b, c){
    Promise.all([
        somePromise(a),
        someOtherPromise(b)
    ]).then(function (data) {
        Promise.all([
            anotherPromise(data[0]),
            yetanotherPromise(data[1])
        ]).then(function (finalResult) {
            console.log('check out finalResult: ', JSON.stringify(finalResult)); // i see the expected results here
            return finalResult;
        }).catch(err => { return err });
    }).catch(err => { return err });
};

为什么 console.log('log the result: ', JSON.stringify(result)); 返回空?换句话说,为什么这一行在promise.all完成之前就被执行了? 我怎样才能让它等待所有 promise 然后执行?

最佳答案

不确定您想要返回哪个 Promise,但总体思路是返回 Promise.all,而不是仅仅调用 Promise.all。

const myPromises = function myPromises(a, b, c){
  return Promise.all([

关于javascript nodejs promise 所有内容稍后执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49420361/

相关文章:

angularjs - 你能在返回之前解析 angularjs 的 promise 吗?

javascript - 通过参数过滤参数,为什么这不起作用?

javascript - JSDoc 中的动态@returns,基于@param 值

javascript - 默认 Javascript 对象很大时速度很慢?

javascript - 我的 Firebase 网站始终只允许一名用户登录,为什么?

javascript - 在 Node.Js 中的 express-validator 之前 trim 输入值

javascript - jQuery 下拉菜单 : not showing one item

node.js - 在 Mongoose/node 中保存对象(使用默认值)

JavaScript:Selenium 在我输入密码之前偶尔会单击登录按钮

javascript - 递归异步函数中的 Promise