Promise handler undefined

Very simple, it seems that the implementation of the implementation based on Promise does not return the value / data that I expect to see.

This is how I expect 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 register a model return, I see the following:

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

When I see _fulfillmentHandler0: undefined , it means that there is no execution handler: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

But the execution handler seems to be present:

 return sdk.request(options).then(function (value) { // fulfillment handler, right? return value; }); 
+6
source share
1 answer

The handler then creates a new promise object , and it will be returned so that promises can be bound.

Quote bluebird documentation for then ,

Returns a new promise, chained from this promise.


In your case

 sdk.request(options) 

returns a promise object and has an execution handler, which is the next then handler.

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

but the then handler returns a new promise object that does not yet have an execution handler. This is why _fulfillmentHandler0 undefined .

You can confirm it as follows

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

will print

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

since promise does not yet have an execution handler. But when you attach a handler to it, like this

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

will print

 { _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 } 

Note 1: There can be more than one execution handler in a single promise object. This is why the output above shows [Function] , which means an array of functions.

Note 2: You do not need to worry about the properties of Promise objects. These are implementation details.


According to the last comment ,

I want to register / check the data / value returned by sdk.request

Yes, you can do it very well with bluebird. You can just tap promise, and you can print the actual allowed value, for example

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

will print the actual allowed value, and you can hook up a then handler to handle the allowed value.

+7
source

Source: https://habr.com/ru/post/988799/


All Articles