3
Most of the implementations I see of Promises (promises) in frameworks Javascript treats the use of Promises in a way that it is possible to access the functions responsible for rejection and resolution in any scope.
For example, Angular:
var defer = $q();
setTimeout(function () {
defer.resolve({status: true});
}, 5000);
defer.promise.then(function (data) {
console.log(data.status);
});
Something similar is possible to do in jQuery.
However, when using the Promise
Javascript native, I can’t visualize how I could do such an operation, unless the call was within the callback passed as argument.
To do in Javascript, I would have to do so, theoretically:
var p = new Promise(function (reject, resolve) {
setTimeout(() => p({status: true}, 5000);
});
p.then((data) => console.log(data.status));
My question is whether there is any way to do the same operation, using the Promise
Javascript native, the same way I did in the first example. Because depending on the structure of the project, it could be horrible to have to be held hostage from encapsulating something inside a callback to have the functionality of Promise
. And from what I’ve been doing on the Internet, it seems that Promise
Javascript was meant to be used anyway. :\
Is there any way simple to circumvent this limitation of the implementation of Javascript promises?
Note: I tried using the call Promise.resolve(promise)
, but I didn’t get the desired effect.
@Wailacemaxters, you want some additional information in the reply? :-)
– Luiz Felipe