javascript nodejs承诺稍后执行所有

javascript nodejs承诺稍后执行所有,javascript,node.js,promise,Javascript,Node.js,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(r

我有很多这样的承诺:

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.all,而不是只调用promise.all

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

我不确定你想回报哪一个承诺,但总的想法是回报承诺。全部在那里,而不是仅仅称之为承诺。全部

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

问题是你的第二个承诺。all没有返回任何东西以将其传递给主承诺。all 试试这段经过编辑的代码

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){
    return Promise.all([ //missing return
        somePromise(a),
        someOtherPromise(b)
    ]).then(function (data) { //data from first promise all
        return Promise.all([ //missing return: passes data to next then
            anotherPromise(data[0]),
            yetanotherPromise(data[1])
        ])
     })
     .then(function (finalResult) { //data from second promise all
            console.log('check out finalResult:',JSON.stringify(finalResult)); // i see the expected results here
            return finalResult; //passes data to next then or top level return whichever available
    })
    .catch(err => { return err }); // top level catch works for the entire chain
};

您的代码不起作用的原因是承诺链是单流,您没有返回第二个承诺中的任何内容以将其传递给外部承诺。所有链

问题是您的第二个承诺。all没有返回任何内容以将其传递给主承诺。all 试试这段经过编辑的代码

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){
    return Promise.all([ //missing return
        somePromise(a),
        someOtherPromise(b)
    ]).then(function (data) { //data from first promise all
        return Promise.all([ //missing return: passes data to next then
            anotherPromise(data[0]),
            yetanotherPromise(data[1])
        ])
     })
     .then(function (finalResult) { //data from second promise all
            console.log('check out finalResult:',JSON.stringify(finalResult)); // i see the expected results here
            return finalResult; //passes data to next then or top level return whichever available
    })
    .catch(err => { return err }); // top level catch works for the entire chain
};

代码不起作用的原因是承诺链是单流,你没有从第二个承诺中返回任何东西,让它传递给外部承诺。所有链

@moaningalways你总是返回两个
承诺。所有
s?@moaningalways你总是返回两个
承诺。所有
s?我很确定你不想要
.catch(err=>{return err})
我很确定您不想要
.catch(err=>{returnerr})