那么()在所有情况下真的回报承诺吗?
我了解然后()
Promise方法总是返回承诺。诺言是我仍然习惯了一些新手的东西,所以我只是在介绍一些例子和修补。以下示例表明的返回值,然后
方法是对象。
const foo = true;
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
if (foo) {
resolve('Do this');
} else {
reject(Error("Do that"));
}
}, 1500)
})
const bar = myPromise.then(value => value);
console.log(typeof bar); // object
这与返回诺言一样吗?还是我需要明确地告诉然后
返回这样的诺言(伪代码,我知道不正确):
.then(value => {
// do something
return new Promise;
})
我想确保我已经知道了。非常感谢帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
然后()
方法返回承诺。它最多需要两个参数:回调功能,以实现承诺的成功和失败情况。承诺是一个对象,承诺的类型将是
对象
。您无需明确归还承诺。您可以阅读有关
Promises
在这里Yes, The
then()
method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.And the promise is an object and the typeof promise will be an
object
. You don't need to explicit return a promise.You can read more about the
promises
here是的
然后()
始终返回承诺。但是,伪代码不正确。函数然后
进行两个参数:与
Promise
构造函数完全一样。 <代码>然后从另一个人创建一个承诺。我们通常会省略on Rexeded
参数,因为我们使用catch
处理拒绝案例。在这里,一篇有趣的文章:
和
然后
doc: https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/global_obignts/global_objects/promise/promise/promise/thenYeah
then()
always return a promise. However, the pseudocode is incorrect. The functionthen
takes two arguments:Exactly like the
Promise
constructor.Then
create a promise from another one. We often omit theonRejected
argument because of we handle the reject case withcatch
.Here an interesting article: https://medium.com/@kevinyckim33/what-are-promises-in-javascript-f1a5fc5b34bf
And the
then
doc : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then