那么()在所有情况下真的回报承诺吗?

发布于 2025-02-07 06:29:40 字数 711 浏览 1 评论 0 原文

我了解然后() 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;
})

我想确保我已经知道了。非常感谢帮助。

I was of the understanding that the then() promise method always itself returns a promise. Promises are something I'm still getting used to being a bit of a newb, so I've just been going over some examples and tinkering. The following example shows that the return value of the then method is an object.

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

Is this the same as returning a promise? Or do I need to explicitly tell then to return a promise like so (pseudocode, I know not correct) :

.then(value => {
    // do something 
    return new Promise;
})

I want to make sure I've got this. Really appreciate the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

善良天后 2025-02-14 06:29:40

是的,然后()方法返回承诺。它最多需要两个参数:回调功能,以实现承诺的成功和失败情况。

承诺是一个对象,承诺的类型将是对象。您无需明确归还承诺。

您可以阅读有关 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

年少掌心 2025-02-14 06:29:40

是的然后()始终返回承诺。但是,伪代码不正确。函数然后进行两个参数:

p.then(onFulfilled[, onRejected]);

p.then(value => {
  // fulfillment
}, reason => {
  // rejection
});

Promise 构造函数完全一样。 <代码>然后从另一个人创建一个承诺。我们通常会省略 on Rexeded 参数,因为我们使用 catch 处理拒绝案例。

在这里,一篇有趣的文章:

然后 doc: https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/global_obignts/global_objects/promise/promise/promise/then

Yeah then() always return a promise. However, the pseudocode is incorrect. The function then takes two arguments:

p.then(onFulfilled[, onRejected]);

p.then(value => {
  // fulfillment
}, reason => {
  // rejection
});

Exactly like the Promise constructor. Then create a promise from another one. We often omit the onRejected argument because of we handle the reject case with catch.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文