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

北城余情 提交于 2021-02-08 11:58:17

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/61510453/js-promises-is-doing-returnvalue-in-a-then-block-the-same-as-resolving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!