Javascript 如何检查 Promise 是否挂起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36294109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:53:25  来源:igfitidea点击:

How to check if a Promise is pending

javascriptpromiseecmascript-6es6-promise

提问by Jeanluca Scaljeri

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

我有这种情况,我想知道承诺的状态是什么。下面,该函数startsomeTest在不再运行时调用(Promise 未挂起)。该start函数可以被多次调用,但如果在测试仍在运行时调用它,它不会等待并返回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.isPendingwould be nice :) Any help would be appreciated!

我找不到一种方法来简单地检查承诺的状态。类似的东西this.promise.isPending会很好:) 任何帮助将不胜感激!

采纳答案by Amit

You can attach a thenhandler that sets a doneflag on the promise (or the RunTestinstance if you prefer), and test that:

您可以附加一个在承诺(或您喜欢的实例)上then设置done标志的处理程序RunTest,并测试:

     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.

请注意空catch()处理程序,无论 Promise 的结果如何,都必须调用处理程序。您可能希望将其包装在一个函数中以保持代码干燥。

回答by Daniel Graham

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);