JS承诺:在`then`块中执行`return(value)`与解析相同吗? [英] JS Promises: is doing `return(value)` in a `then` block the same as resolving?

查看:29
本文介绍了JS承诺:在`then`块中执行`return(value)`与解析相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

我用 1 解决最初的承诺.然后在 then 块中执行 return 2 .这样返回的承诺值是否为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?

推荐答案

是.调用 .then 会创建一个新的Promise,该Promise将解析为您在回调中返回的任何内容.

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

这篇关于JS承诺:在`then`块中执行`return(value)`与解析相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆