Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
JavaScript: How to make a promise that never resolves or rejects

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda