javascript - Is there a way to tell if an ES6 promise is fulfilled/rejected/resolved? -


this question has answer here:

i'm used dojo promises, can following:

promise.isfulfilled(); promise.isresolved(); promise.isrejected(); 

is there way determine if es6 promise fulfilled, resolved, or rejected? if not, there way fill in functionality using object.defineproperty(promise.prototype, ...)?

they not part of specification nor there standard way of accessing them use internal state of promise construct polyfill. however, can convert standard promise 1 has these values creating wrapper,

function makequerablepromise(promise) {     // don't create wrapper promises can queried.     if (promise.isresolved) return promise;      var isresolved = false;     var isrejected = false;      // observe promise, saving fulfillment in closure scope.     var result = promise.then(        function(v) { isresolved = true; return v; },         function(e) { isrejected = true; throw e; });     result.isfulfilled = function() { return isresolved || isrejected; };     result.isresolved = function() { return isresolved; }     result.isrejected = function() { return isrejected; }     return result; } 

this doesn't affect promises, modifying prototype would, allow convert promise promise exposes state.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -