How to solve or reject a (native) Javascript Promise outside your scope?

2

Most of the implementations I see from promises in Javascript frameworks treat the use of Promises so 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 can be done in jQuery.

However, when using the native Javascript Promise, I can not see how I could do such an operation, unless the call occurred within the last callback as a parameter.

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 native Javascript Promise, the same way I did in the first example. For, depending on the design structure, it could be horrible to have to be held hostage to encapsulate something inside a callback to have Promise functionality. And from what I've been searching on the internet, it looks like Javascript Promise was meant to be used anyway:

Is there any [simple] way to circumvent this limitation in Javascript?

Note: I tried to use the Promise.resolve(promise) call but did not get the desired effect.

    
asked by anonymous 22.05.2018 / 17:20

0 answers