Promise履行处理程序未定义 [英] Promise fulfillment handler undefined

查看:183
本文介绍了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 时,似乎暗示没有履行处理程序: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise /然后

When I see _fulfillmentHandler0: undefined that seems to imply that there is no fulfillment handler: https://developer.mozilla.org/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关于<$ c $的文档c>然后


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

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履行处理程序未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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