In my own projects, async/await has made improved readbility and reduced errors.
In my own projects, async/await has made improved readbility and reduced errors.
1) Not calling promises in parallel. Easy to do because it's impossible to run them in parallel with just "await", need to use Promise.all() or something.
2) Forgetting to write "await". If you try to use the return value then now you have a Promise object instead of the actual value. But the worst is when the code doesn't use the return value. Then it has a really subtle race condition that's hard to find. Or if the promise has any errors, then you might get the dreaded "Unhandled promise rejection" with a useless stack trace, and you might need to search the entire codebase to find where it happened.
const [foo, bar] = await [async1(), async2()] const [foo, bar] = await Promise.all([async1(), async2()]) const xP = getX();
const yP = getY();
const x = await xP;
const y = await yP;
is just as parallel as const [x, y] = Promise.all([getX(), getY()]);Async/await is pure syntactic sugar and should not reduce errors unless the Promise syntax was implemented incorrectly.
This an implementation issue. Of course you can wrap the non-conformant external code but in this case that means accepting anti-patterns in order to gain the benefit of syntactic sugar. Writing API wrappers that do nothing more than wrap traditional Promise callback code as a rule is a poor substitute for allowing developers to choose the most appropriate implementation. Instead that would favor meaningless promise chaining and unless you ignore a library’s type definitions that is pretty hard to miss. Using async/await isn’t doing anything different in the same sense that using a class is no different than using a function that returns an object. The question is one of readability and flexibility, no one syntax is inherently better that the other because the interpreter does not know the difference.