Live data from Hacker News

Mistakes we make using JavaScript Promises

betamark.com

21–30 of 60 posts

Re: Mistakes we make using JavaScript Promises

#21

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…

I think it could be a linter rule to force async functions await any calls to async/promise functions. Or an explicit way to ignore it.

Re: Mistakes we make using JavaScript Promises

#22

Hopefully 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…

I've seen a lot of developers at the place I work make the mistake you outline in #1. Always something I'm watching out for in code reviews...

Re: Mistakes we make using JavaScript Promises

#23

Hopefully 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…

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

#24

Hopefully 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…

TypeScript helps immensely in this case.

Re: Mistakes we make using JavaScript Promises

#25
post #23

Earlier 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()]

You can use await on the promise returned by Promise.all()

Re: Mistakes we make using JavaScript Promises

#26

One 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.

...or call .then() on it.

Re: Mistakes we make using JavaScript Promises

#27

This "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…

Instead of subtle nesting, I would have passed through the user as part of the result of the second promise.

    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

#28
post #19

Are 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.

If it's callback driven, is it a Promise mistake or a callback mistake? I don't think it would apply here

Re: Mistakes we make using JavaScript Promises

#29

Earlier 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.

The API in this example is promise-friendly, it is just intended for callback syntax; providing two situational callback options.

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

#30
post #23

Earlier 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()]

Yes, the correct syntax is:

    const [foo, bar] = await Promise.all([async1(), async2()])
Post reply on HN