Skip to content
Advertisement

Difference between two ways of resolving a promise

I would like to understand the difference between the two code snippets, both resolving promises in different ways but receiving the same result (resolving after a second).

Which one is the right way?

JavaScript

JavaScript

Advertisement

Answer

In this case, where you’re not resolving with a value, there is no difference. In situations where you need to resolve the promise with a value, you would want to use the first example so you can control what is passed into the resolve function.

JavaScript

Edit: Or you can use the second example and pass the resolved value as the third argument to setTimeout. thanks to VLAZ for pointing this out.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement