0

I am working with Angular JS and need to know the status of a Promise object whether it is in Pending/Fulfilled/Rejected status?

In Chrome Developer tool ( v 60 ), What I currently do is

  1. Select that variable and choose stored as global variable (temp1)

     temp1;
     Deferred {promise: Promise, resolve: ƒ, reject: ƒ, notify: ƒ}
    
  2. write below statement to check the Promise object status .

    temp1.then(() => { console.log('resolved'); })
         .catch(()=> { console.info(''rejected'); });
    

Although this is working fine. But I am looking for any alternative / different approach which does the same but in shorter way.

temp1.isRejected // return true/false
temp1.isResolved // return true/false

I also checked one suggestion that we can write method in the console and can run with this temp1 variable but again for this, I need to write the method on each new tab which is also cumbersome.

any suggestion or help?

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • What promise library created the Deferred object? If the library doesn't provide synchronous inpection of promise objects' status and value you are out of luck. Google Chrome inspection of its native Promise object internals is a proprietary extension of Promise implementation and not based on ES6 promise specifications. – traktor Sep 09 '17 at 06:19
  • The screenshot shows "promise" property so it might just do what you look for. – wOxxOm Sep 09 '17 at 07:06

1 Answers1

4

In the case where you are using Chrome's native Promises, you can just look at the Promise's [[PromiseStatus]] property:

enter image description here


With standard $q promises in AngularJS (I assume you are talking about Angular 1.x), you can check the $$state property:

enter image description here

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • I do not have native `Promise` but `Deferred` Object which having different properties – xkeshav Sep 09 '17 at 05:49
  • @pro.mean Updated to include standard `$q` promises in Angular. If your `Deferred` objects are from other libraries, you will have check its implementation. – Derek 朕會功夫 Sep 09 '17 at 06:18
  • Thanks for the insight, also get help from [This SO answer](https://stackoverflow.com/questions/24091513/get-state-of-angular-deferred) – xkeshav Sep 09 '17 at 07:22
  • The Chrome debugger (v 69) doesn't show Promise status correctly. It shows "pending" even when the Promise has already resolved, and I know it has already resolved because I have the resolved value from the Promise and because program execution has passed "await myPromise". – N73k Oct 11 '18 at 16:34
  • @N73k It works on my machine. Are you sure you were looking at the correct Promise object? – Derek 朕會功夫 Oct 11 '18 at 16:41
  • Yes. It was a simple test case I set up. But I was using node.js v 8.11.4 with the Chrome debugger (v 69). I wasn't running the code in Chrome. So maybe some kind of problem between node.js and the Chrome debugger. – N73k Oct 11 '18 at 16:59
  • @N73k That’s a possibility. – Derek 朕會功夫 Oct 11 '18 at 17:05