Node.js中Promise.all执行延迟及Promise返回值为空问题排查
Hey there, let's break down exactly what's going wrong with your Promise code and fix it up properly.
The Core Problems
Your
myPromisesfunction doesn't return a Promise
Right now,myPromisesruns asynchronous operations but never returns the resulting Promise to the upper chain. That means when your upper.then(result => ...)runs, it's gettingundefined(the default return value of a function with no explicit return) instead of the final async result. The upper chain doesn't wait formyPromises's work to finish either—hence the "execution delay" feeling (it's not delayed, it's just not waiting).Nested Promises don't pass results upward
Even insidemyPromises, you're callingPromise.all()but not returning the innerPromise.allto 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
myPromisescompletes. - Result propagation: The final
finalResultis passed all the way up the chain to the upperconsole.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




