如何处理 Promise.race() 中的拒绝而不使 Promise 解析
How to handle reject in Promise.race() without making the Promise resolve
我想创建一个仅在 Promise 解析时 运行s 的函数,并且在它拒绝或达到超时时不执行任何操作。
这是我的想法:
onlyRunIfResolvesInTime().then(function(){
// only run if resolved
})
不幸的是,以下代码在达到超时时总是抛出 Uncaught (in promise) two
错误(promise2
拒绝)。
// This promise would be replaced with a function
// that only can resolve under certain conditions,
// but if it can't in time we want to reject.
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, "one")
})
// This promise is the timeout that rejects if the
// time limit is reached.
const promise2 = new Promise((resolve, reject) => {
setTimeout(reject, 100, "two")
})
let onlyRunIfResolvesInTime = function () {
return Promise.race([promise1, promise2])
}
onlyRunIfResolvesInTime()
.then(() => {
console.log("running function")
})
如果我在 Promise.race() 中发现错误,如下所示
let onlyRunIfResolvesInTime = function () {
return Promise.race([promise1, promise2])
.catch(() => { })
}
然后我的 onlyRunIfResolvesInTime
函数总是解析并且 运行 是 then
函数,而不是在达到超时时什么都不做。
如果 Promise.race()
解决并忽略拒绝,如何让 onlyRunIfResolvesInTime
仅 运行?
您可以使用一个变量:
result = promise.then(
function(v) {
isRejected = false;
isPending = false;
return v;
},
function(e) {
isRejected = true;
isPending = false;
throw e;
}
);
function e() {
if(isPending===false){
setTimeout(10, e)
} else if(isRejected===true){
//rejected
} else if(isRejected===false){
//fulfilled
}
}
e()
忽略 onlyRunIfResolvesInTime
的拒绝句柄
onlyRunIfResolvesInTime()
.then(() => {
console.log("running function")
})
.catch(() => null) // timed out -> do nothing
我想创建一个仅在 Promise 解析时 运行s 的函数,并且在它拒绝或达到超时时不执行任何操作。
这是我的想法:
onlyRunIfResolvesInTime().then(function(){
// only run if resolved
})
不幸的是,以下代码在达到超时时总是抛出 Uncaught (in promise) two
错误(promise2
拒绝)。
// This promise would be replaced with a function
// that only can resolve under certain conditions,
// but if it can't in time we want to reject.
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, "one")
})
// This promise is the timeout that rejects if the
// time limit is reached.
const promise2 = new Promise((resolve, reject) => {
setTimeout(reject, 100, "two")
})
let onlyRunIfResolvesInTime = function () {
return Promise.race([promise1, promise2])
}
onlyRunIfResolvesInTime()
.then(() => {
console.log("running function")
})
如果我在 Promise.race() 中发现错误,如下所示
let onlyRunIfResolvesInTime = function () {
return Promise.race([promise1, promise2])
.catch(() => { })
}
然后我的 onlyRunIfResolvesInTime
函数总是解析并且 运行 是 then
函数,而不是在达到超时时什么都不做。
如果 Promise.race()
解决并忽略拒绝,如何让 onlyRunIfResolvesInTime
仅 运行?
您可以使用一个变量:
result = promise.then(
function(v) {
isRejected = false;
isPending = false;
return v;
},
function(e) {
isRejected = true;
isPending = false;
throw e;
}
);
function e() {
if(isPending===false){
setTimeout(10, e)
} else if(isRejected===true){
//rejected
} else if(isRejected===false){
//fulfilled
}
}
e()
忽略 onlyRunIfResolvesInTime
onlyRunIfResolvesInTime()
.then(() => {
console.log("running function")
})
.catch(() => null) // timed out -> do nothing