最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript: How to make a promise that never resolves or rejects - Stack Overflow

programmeradmin0浏览0评论

I am using:

 await Promise.race([promise1, promise2]);

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.

I tried

async function promise2(timeout=5000) {
    await new Promise(resolve => setTimeout(resolve, timeout));
    if (didStuffAndOK())  {
        return "OK"
    }
    return new Promise( () => {} )
}

return new Promise( () => {} ) seems to be interpreted as the promise being rejected rather than never resolved.

How does one make an empty promise (not in real life, in JavaScript)?

I am using:

 await Promise.race([promise1, promise2]);

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.

I tried

async function promise2(timeout=5000) {
    await new Promise(resolve => setTimeout(resolve, timeout));
    if (didStuffAndOK())  {
        return "OK"
    }
    return new Promise( () => {} )
}

return new Promise( () => {} ) seems to be interpreted as the promise being rejected rather than never resolved.

How does one make an empty promise (not in real life, in JavaScript)?

Share Improve this question asked Jan 25, 2022 at 10:01 run_the_racerun_the_race 2,4183 gold badges54 silver badges84 bronze badges 3
  • Wouldn't it be easier to make promise1 reject if timeout is reached and wrap it up with try/catch and fallback to promise when catch clause is invoked? – Aitwar Commented Jan 25, 2022 at 10:05
  • Well that behaviour is different, because if promise2 doesnt work, I wish to give promise1 its time to continue. promise1 is a fetch, then let it take longer if theres not cache hit from promise 2 – run_the_race Commented Jan 25, 2022 at 10:07
  • promise1 is a fetch, then let it take longer if theres not cache hit from promise 2 Is promise2 meant as a 'cache' for promise1 results ? In that case, chaining should solve it: start with promise2, if if fails proceed with promise1. Even better: make the caching transparent by moving it inside promise1 itself. – Alex Commented Jan 25, 2022 at 10:15
Add a ment  | 

1 Answer 1

Reset to default 10

To answer straight your question new Promise( () => {} ) never resolves.

Here is the prof :

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

But I guess you have a diffrent question.

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.

For this you can resolve in the promise2 the promise1, because promises are chainable.

(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');
})()

发布评论

评论列表(0)

  1. 暂无评论