Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Promise is pending [duplicate]

I have this situation in which I would like to know what the status is of a promise. Below, the function start only calls someTest if it is not running anymore (Promise is not pending). The start function can be called many times, but if its called while the tests are still running, its not going to wait and returns just false

class RunTest {
    start() {
         retVal = false;

         if (!this.promise) {
             this.promise = this.someTest();
             retVal = true;                
         }

         if ( /* if promise is resolved/rejected or not pending */ ) {
             this.promise = this.someTest();
             retVal = true;
         }

         return retVal;
    }

    someTest() {
        return new Promise((resolve, reject) => {
            // some tests go inhere
        });
    }
}

I cannot find a way to simply check the status of a promise. Something like this.promise.isPending would be nice :) Any help would be appreciated!

like image 756
Jeanluca Scaljeri Avatar asked Mar 29 '16 20:03

Jeanluca Scaljeri


People also ask

How do I know if my promise state is pending?

js, we will get either 'Promise { <pending> }' while pending or 'Promise { undefined }' when finished. Now if we check the string for the word pending we could define the state and check if a promise is pending or not using the following one line: util. inspect(myPromise). includes("pending") .

How do I know if JavaScript promise is fulfilled?

The best place to know if a promise is resolved is in . then(). Testing if a Promise is fullfilled would create a polling loop which is most likely the wrong direction. async/await is a nice construct if you'd like to reason async code synchronously.

How do you deal with a pending promise?

The promise will always log pending as long as its results are not resolved yet. You must call . then on the promise to capture the results regardless of the promise state (resolved or still pending): Promises are forward direction only; You can only resolve them once.

What happens if you resolve a promise twice?

No. It is not safe to resolve/reject promise multiple times. It is basically a bug, that is hard to catch, becasue it can be not always reproducible.


2 Answers

You can attach a then handler that sets a done flag on the promise (or the RunTest instance if you prefer), and test that:

     if (!this.promise) {
         this.promise = this.someTest();
         this.promise.catch(() => {}).then(() => { this.promise.done = true; });
         retVal = true;                
     }

     if ( this.promise.done ) {
         this.promise = this.someTest();
         this.promise.catch(() => {}).then(() => { this.promise.done = true; });
         retVal = true;
     }

Notice the empty catch() handler, it's crucial in order to have the handler called regardless of the outcome of the promise. You probably want to wrap that in a function though to keep the code DRY.

like image 109
Amit Avatar answered Oct 19 '22 01:10

Amit


class RunTest {
   constructor() {
    this.isRunning = false;
   }
   start() {
      console.log('isrunning', this.isRunning);
      var retVal = false;
      if(!this.isRunning) {
        this.promise = this.someTest();
        this.promise.catch().then(() => { this.isRunning = false; });
        retVal = true;                
      }
      return retVal;
    }
    someTest() {
        this.isRunning = true;
        return new Promise((resolve, reject) => {
          setTimeout(function() {
             //some tests go inhere
             resolve();
           }, 1000);
        });
    }
};

var x = new RunTest();

x.start(); //logs false
x.start(); //logs true

setTimeout(function() {
    //wait for a bit
  x.start(); //logs false
}, 2000);
like image 2
Daniel Graham Avatar answered Oct 19 '22 01:10

Daniel Graham