You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Node.js中Promise.all执行延迟及Promise返回值为空问题排查

Fixing Your Promise Chain Issues: Empty Result & Execution Delay

Hey there, let's break down exactly what's going wrong with your Promise code and fix it up properly.

The Core Problems

  1. Your myPromises function doesn't return a Promise
    Right now, myPromises runs asynchronous operations but never returns the resulting Promise to the upper chain. That means when your upper .then(result => ...) runs, it's getting undefined (the default return value of a function with no explicit return) instead of the final async result. The upper chain doesn't wait for myPromises's work to finish either—hence the "execution delay" feeling (it's not delayed, it's just not waiting).

  2. Nested Promises don't pass results upward
    Even inside myPromises, you're calling Promise.all() but not returning the inner Promise.all to the outer .then, so the final result never makes it out of the nested callbacks.

Fixed Code (Promise Chain Version)

First, let's fix myPromises to properly return Promises at every step, so results flow up to the upper chain:

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)); // Now this will have your data!
    })
};

const myPromises = function myPromises(a, b, c){
  // Return the first Promise.all to link it to the upper chain
  return Promise.all([ somePromise(a), someOtherPromise(b) ])
    .then(function (data) {
      // Return the second Promise.all so its result flows to the next .then
      return Promise.all([ anotherPromise(data[0]), yetanotherPromise(data[1]) ]);
    })
    .then(function (finalResult) {
      console.log('check out finalResult: ', finalResult);
      // Return the final result to pass it up to the upper chain
      return finalResult;
    });
};

Even Cleaner: Async/Await Version

If you can use modern JavaScript, async/await makes this code way easier to read and maintain, with zero nested callbacks:

module.exports = async function (a, b, c) {
  // Wait for someAsync to finish first
  await someAsync(() => { someFunc(a); });
  // Wait for myPromises to complete and get its result
  const result = await myPromises(a, b, c);
  console.log('log the result: ', JSON.stringify(result));
};

const myPromises = async function myPromises(a, b, c){
  // Wait for the first batch of promises
  const [dataFromA, dataFromB] = await Promise.all([ somePromise(a), someOtherPromise(b) ]);
  // Wait for the second batch using the first batch's results
  const finalResult = await Promise.all([ anotherPromise(dataFromA), yetanotherPromise(dataFromB) ]);
  console.log('check out finalResult: ', finalResult);
  // Return the final result to the caller
  return finalResult;
};

Why This Works

  • Explicit returns: Every asynchronous operation now returns its Promise, so the chain properly waits for each step to finish before moving on. No more "delay" because the upper code is no longer running before myPromises completes.
  • Result propagation: The final finalResult is passed all the way up the chain to the upper console.log, so it won't be empty anymore.
  • Cleaner flow: Async/await eliminates callback nesting, making it much easier to track how data moves through your async operations.

内容的提问来源于stack exchange,提问作者moaningalways

火山引擎 最新活动