如何检查 Promise 是否挂起
How to check if a Promise is pending
我有这种情况,我想知道承诺的状态。下面,函数 start
仅在它不再是 运行 时才调用 someTest
(Promise 不是挂起的)。 start
函数可以调用多次,但如果在测试仍在 运行 时调用它,它不会等待 returns 只是 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
});
}
}
我找不到简单地检查承诺状态的方法。像 this.promise.isPending
这样的东西会很好 :) 任何帮助将不胜感激!
您可以附加一个 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;
}
注意空的 catch()
处理程序,无论承诺的结果如何,调用处理程序都是至关重要的。
您可能希望将其包装在一个函数中以保持代码干燥。
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);
我有这种情况,我想知道承诺的状态。下面,函数 start
仅在它不再是 运行 时才调用 someTest
(Promise 不是挂起的)。 start
函数可以调用多次,但如果在测试仍在 运行 时调用它,它不会等待 returns 只是 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
});
}
}
我找不到简单地检查承诺状态的方法。像 this.promise.isPending
这样的东西会很好 :) 任何帮助将不胜感激!
您可以附加一个 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;
}
注意空的 catch()
处理程序,无论承诺的结果如何,调用处理程序都是至关重要的。
您可能希望将其包装在一个函数中以保持代码干燥。
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);