Using async/await Inside a Promise() Constructor: An Anti-Pattern?
In this scenario, you're employing an async.eachLimit function to manage concurrent operations. However, your use of async/await inside the Promise constructor function raises concerns.
Promise Constructor Anti-Pattern
Using promises within the promise constructor's executor function, as you're doing, is considered an anti-pattern. The primary risk, as exemplified in your code, is the potential for errors to slip through unchecked.
Furthermore, async/await introduces additional complexity, potentially obscuring these issues. For instance, in your code:
let p = new Promise(resolve => { ""(); // TypeError resolve(); }); (async () => { await p; })().catch(e => console.log("Caught: " + e)); // Catches it.
Here, the TypeError goes unnoticed within the promise constructor, but it is caught when using async/await. Such surprises can be problematic.
Therefore, while your code allows you to access values in nested callbacks, it's essential to handle errors carefully and avoid these anti-patterns to ensure robust and maintainable code.
The above is the detailed content of Is Using async/await Inside a Promise Constructor an Anti-Pattern?. For more information, please follow other related articles on the PHP Chinese website!