-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stack trace is lost from the point the code uses p-map #34
Comments
This is, unfortunately, a limitation of JavaScript: https://stackoverflow.com/questions/56644392/passing-an-async-function-as-a-callback-causes-the-error-stack-trace-to-be-lost The only way to solve this is to:
|
Thanks @sindresorhus .
TS version of the above.
|
I looked into this a little bit this morning. Running @davidfirst's examples, I'm able to see the short and long stacktraces. From @sindresorhus's comment, I would have expected that the problem was being caused because we were catching the error on line 59, then passing it to I created a simplified version of the const fakepmap = async (f) => {
return new Promise((resolve, reject) => {
const next = () => {
(async () => {
try {
resolve(await f());
} catch (error) {
reject(error);
}
})();
};
next();
});
}
const promiseFn = async () => {
throw new Error('stop');
};
fakepmap(promiseFn).then(console.log).catch(console.log);
// Error: stop
// at promiseFn (/Users/me/Source/test.js:19:9)
// at /Users/me/Source/test.js:7:25
// at next (/Users/me/Source/test.js:11:9)
// at /Users/me/Source/test.js:14:5
// at new Promise (<anonymous>)
// at fakepmap (/Users/me/Source/test.js:2:10)
// at Object.<anonymous> (/Users/me/Source/test.js:22:1)
// at Module._compile (internal/modules/cjs/loader.js:1138:30)
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
// at Module.load (internal/modules/cjs/loader.js:986:32) Any idea what is missing from this test scenario but is present in |
I do not know the exact thing but your implementation does not limit the concurrency, maybe that could be an issue. It would be really great to have proper stack traces but so far I have not encountered anything which properly solves this so I assume your implementation omits some step which loses the stack trace but is required for limiting the concurrency. You could also take a look at p-limit. Its code is a decent bit shorter but suffers from the same issue. It might be easier to figure out which step exactly loses the stack trace in that implementation. |
Why do you think so? |
My answer was in response to the snippet from @marchuffnagle, not yours |
Oh sorry, I missed that. |
Both,
console.trace
andthrow new Error
don't show the stack-trace beyond the point p-map got called. In other words, the functions that called before pMap, disappear from the stack trace.As a comparison, the p-map-series doesn't suffer from this issue. It does keep the stack-trace.
See the example below, if you run a function that runs the native
Promise.all
, the stack trace shows the function name -runPromiseNative
. Same if you run the functionrunPromisePmapSeries
. However, try to runrunPromisePmap
, and you'll see how the stack trace truncated.I tried node v12.x and v14.x.
Results when running
runPromisePmap
:Results when running
runPromiseNative
.The text was updated successfully, but these errors were encountered: