JavaScript: 如何做出永不解决或拒绝的承诺
JavaScript: How to make a promise that never resolves or rejects
我正在使用:
await Promise.race([promise1, promise2]);
逻辑是,如果promise1
在5s内没有resolved/rejected,那么promise2
或许可以解决。因此,在延迟 promise2
尝试做它的事情之后,如果它失败了,我希望 promise2
return 一个永远不会解析的 Promise
以便它全部等待 promise1
.
我试过了
async function promise2(timeout=5000) {
await new Promise(resolve => setTimeout(resolve, timeout));
if (didStuffAndOK()) {
return "OK"
}
return new Promise( () => {} )
}
return new Promise( () => {} )
似乎被解释为承诺被拒绝而不是从未解决。
如何做出空头支票(不是在现实生活中,在JavaScript)?
直接回答你的问题new Promise( () => {} )
永远无法解决。
这是教授:
new Promise(()=>{})
.then(()=> console.log('promise resolved'))
.catch(()=>console.log('promise rejected'));
console.log('FOO, so that you can see that the code was executed');
但我想你有一个不同的问题。
The logic is if promise1 has not resolved/rejected within 5s, then promise2
might be able to resolve. So after a delay promise2 tries to do its thing, if it
fails I wish to have promise2 return a Promise that never resolves so that its
all up to waiting for promise1.
为此,您可以在 promise2
中解析 promise1
,因为 promises 是可链接的。
(async ()=>{
const promise1 = new Promise(()=>{}) // simulating here a very long fetch;
const promise2 = new Promise((res)=>{
setTimeout(()=>{
try {
console.log('one second passed and the fetch is still running');
throw Error() // simulating a promise2 does it thing and fails
} catch (e){
res(promise1); // Chain promise1 back
}
}, 1000);
});
await Promise.race([promise1, promise2]);
console.log('This won\'t be printed because the previous line awaits for promise1');
})()
我正在使用:
await Promise.race([promise1, promise2]);
逻辑是,如果promise1
在5s内没有resolved/rejected,那么promise2
或许可以解决。因此,在延迟 promise2
尝试做它的事情之后,如果它失败了,我希望 promise2
return 一个永远不会解析的 Promise
以便它全部等待 promise1
.
我试过了
async function promise2(timeout=5000) {
await new Promise(resolve => setTimeout(resolve, timeout));
if (didStuffAndOK()) {
return "OK"
}
return new Promise( () => {} )
}
return new Promise( () => {} )
似乎被解释为承诺被拒绝而不是从未解决。
如何做出空头支票(不是在现实生活中,在JavaScript)?
直接回答你的问题new Promise( () => {} )
永远无法解决。
这是教授:
new Promise(()=>{})
.then(()=> console.log('promise resolved'))
.catch(()=>console.log('promise rejected'));
console.log('FOO, so that you can see that the code was executed');
但我想你有一个不同的问题。
The logic is if promise1 has not resolved/rejected within 5s, then promise2
might be able to resolve. So after a delay promise2 tries to do its thing, if it
fails I wish to have promise2 return a Promise that never resolves so that its
all up to waiting for promise1.
为此,您可以在 promise2
中解析 promise1
,因为 promises 是可链接的。
(async ()=>{
const promise1 = new Promise(()=>{}) // simulating here a very long fetch;
const promise2 = new Promise((res)=>{
setTimeout(()=>{
try {
console.log('one second passed and the fetch is still running');
throw Error() // simulating a promise2 does it thing and fails
} catch (e){
res(promise1); // Chain promise1 back
}
}, 1000);
});
await Promise.race([promise1, promise2]);
console.log('This won\'t be printed because the previous line awaits for promise1');
})()