Hopefully with async/await we can all put this behind us. In my own projects, async/await has made improved readbility and reduced errors.
I have found cases in which async/await won’t cut it quite as cleanly as “return new Promise”. AWS Cognito’s API has functions that take an object parameter with properties onSuccess and onFailure. If these functions are being called with programmatically created values within promises you can use “await new Promise” but that means declaring an additional async/await function (and therefore a promise) that resolves w…
Mistakes we make using JavaScript Promises
21–30 of 60 posts
Re: Mistakes we make using JavaScript Promises
#22Hopefully with async/await we can all put this behind us. In my own projects, async/await has made improved readbility and reduced errors.
async/await helps a lot, but there are still a few common errors with them... 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 va…
Re: Mistakes we make using JavaScript Promises
#23Hopefully with async/await we can all put this behind us. In my own projects, async/await has made improved readbility and reduced errors.
async/await helps a lot, but there are still a few common errors with them... 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 va…
const [foo, bar] = await [async1(), async2()]Re: Mistakes we make using JavaScript Promises
#24Hopefully with async/await we can all put this behind us. In my own projects, async/await has made improved readbility and reduced errors.
async/await helps a lot, but there are still a few common errors with them... 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 va…
Re: Mistakes we make using JavaScript Promises
#25Earlier quoted context omitted.
async/await helps a lot, but there are still a few common errors with them... 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 va…
For the first I wonder if we could solve this with array syntax: const [foo, bar] = await [async1(), async2()]
Re: Mistakes we make using JavaScript Promises
#26One thing I realized about async/await is that it removes the ability to use the synchronous continuation of an async function call. In return, it makes its asynchronous continuation feel synchronous. Technically, that's less power, but I realized that's almost always a good thing.
I don't understand what you're saying here. An async function just returns a Promise, that's it. You don't have to await on it immediately - you could just as easily assign it to a variable and await on it some time later.
Re: Mistakes we make using JavaScript Promises
#27This "solution" given here has a syntax error since you don't have access to `user` within the second then() callback: get("http://data.com/user") .then(user => get("http://data.com/location" + user.id)) .then(location => createEntry(user, location)) .then(response => { // handle response }).catch(err => { // handle failure }); Instead, back before async/await made things easier this nested pattern was used (notice t…
get('//data.com/user)
.then(user => resolve({user, location: get('//data.com/user')}))
.then(({user, location}) => createEntry(user, location))
.then(response => {
// handle response
}).catch(err => {
// handle failure
});
Async/await is much cleaner now though.Re: Mistakes we make using JavaScript Promises
#28Are these "common" mistakes? With basic understanding of Promises, these mistakes shouldn't happen. Also I believe #1 is wrong when using async/await.
Some older libraries will still be callback driven, and one important thing is always resolving or rejecting promise.
Re: Mistakes we make using JavaScript Promises
#29Earlier quoted context omitted.
I have found cases in which async/await won’t cut it quite as cleanly as “return new Promise”. AWS Cognito’s API has functions that take an object parameter with properties onSuccess and onFailure. If these functions are being called with programmatically created values within promises you can use “await new Promise” but that means declaring an additional async/await function (and therefore a promise) that resolves w…
I'm not sure I fully understand. Interoperability is as simple as wrapping the non-standard API, no? This way you have a standard calling style in your code. It's not a weakness of async/await that some libraries don't have promise friendly APIs.
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.
Re: Mistakes we make using JavaScript Promises
#30Earlier quoted context omitted.
async/await helps a lot, but there are still a few common errors with them... 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 va…
For the first I wonder if we could solve this with array syntax: const [foo, bar] = await [async1(), async2()]
const [foo, bar] = await Promise.all([async1(), async2()])