Promise履行处理程序未定义

编程入门 行业动态 更新时间:2024-10-25 04:19:08
本文介绍了Promise履行处理程序未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

非常基本的,看起来,基于Promise的实现的实现并没有返回我期望看到的值/数据。

Very basic, it seems, implementation of a Promise based implementation is not returning the value/data I expect to see.

这就是我期待这个界面的方式工作:

This is how I am expecting this interface to work:

sdk.request(options) => Promise~Response → Object (JSON)

以下是我模型中的代码:

Here is the code in my model:

return sdk.request(options).then(function (value) { return value; });

当我记录模型的返回时,我看到了:

When I log the return of the model, I see this:

{ _bitField: 0, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _progressHandler0: undefined, _promise0: undefined, _receiver0: undefined, _settledValue: undefined }

当我看到 _fulfillmentHandler0:undefined 时,似乎暗示没有履行处理程序: developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise /然后

When I see _fulfillmentHandler0: undefined that seems to imply that there is no fulfillment handler: developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

但履行处理程序似乎确实存在:

But the fulfillment handler does seem to be present:

return sdk.request(options).then(function (value) { // fulfillment handler, right? return value; });

推荐答案

然后 handler将创建一个新的promise对象,并将返回以便可以链接promises。

The then handler will create a new promise object and that will be returned so that the promises can be chained.

引用 bluebird关于然后 ,

返回从此承诺链接的新承诺。

Returns a new promise chained from this promise.

在您的情况下,

In your case,

sdk.request(options)

返回一个承诺对象,它有一个履行处理程序,它是以下然后处理程序。

returns a promise object and that has a fulfillment handler, which is the following then handler.

.then(function (value) { return value; });

但是然后处理程序返回一个新的承诺对象,还没有履行处理程序。这就是为什么 _fulfillmentHandler0 是未定义。

but the then handler returns a new promise object, which doesn't have a fulfillment handler yet. That is why _fulfillmentHandler0 is undefined.

你可以像这样确认这个

var promise = require("bluebird").resolve(); console.log(promise);

将打印

{ _bitField: 268435456, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _progressHandler0: undefined, _promise0: undefined, _receiver0: undefined, _settledValue: undefined }

,因为承诺还没有履行处理程序。但当你附上一个处理程序时,就像这样

since promise has no fulfillment handler yet. But when you attach a handler to it, like this

var promise1 = promise.then(function () {}) console.log(promise);

将打印

{ _bitField: 268435457, _fulfillmentHandler0: [Function], _rejectionHandler0: undefined, _progressHandler0: undefined, _promise0: { _bitField: 0, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _progressHandler0: undefined, _promise0: undefined, _receiver0: undefined, _settledValue: undefined }, _receiver0: undefined, _settledValue: undefined }

注1 :单个promise对象可以有多个履行处理程序。这就是为什么上面的输出显示 [Function] ,这意味着一系列函数。

Note 1: A single promise object can have more than one fulfillment handlers. That is why the output above shows [Function], which means an array of functions.

注意2:您不必担心Promise对象的属性。这些是实施细节。

Note 2: You don't have to worry much about the properties of the Promise objects. These are implementation details.

根据最后评论,

我想要记录/验证 sdk.request返回的数据/值

是你可以用蓝鸟很好地做到这一点。您只需 点击 即可保证,你可以打印实际解决的值,比如这个

Yes, you can very well do that with bluebird. You can simply tap the promise and you can print the actual resolved value, like this

return sdk.request(options).tap(console.log);

将打印实际解析的值,您可以附加然后处理已解析值的处理程序。

will print the actual resolved value and you can attach a then handler to process the resolved value.

更多推荐

Promise履行处理程序未定义

本文发布于:2023-11-11 10:47:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   程序   Promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!