Search code examples
javascriptpromisees6-promise

JS Promises: is doing `return(value)` in a `then` block the same as resolving?


I have the following code:

new Promise((resolve, reject) => {
  resolve(1)
}).then(value => {
  return 2
})

I resolve the initial promise with 1. Then in the then block I do return 2. Does this return a promise resolved with the value 2?


Solution

  • Yes. Calling .then creates a new promise, and that promise will resolve to whatever you return in the callback.