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

160
Vistas
Save results in Promise after it's done

I use Promise to do a http post. I can see results been output inside then(). How can I access data later? Tried resultPromise.then(), did not produce same result.

let resultPromise = 
fetch(cleanUrl, {
    method: 'POST', // or 'PUT'
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify(body),
    })
.then(response => response.json())
.then(data => {
    console.log('Success:', data);
})
.catch((error) => {
    console.error('Error:', error);
});
//what I intent to do
if(data.isSafe == ture) {
   //do something
}
else {
    //do somehing
}

Update 1

Added description of what I want to do with data.

Update 2 Move the code inside then() as @Nicholas Tower suggested.

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

0

.then(data => {
  console.log('Success:', data);
})

Since there's no return value here, the promise it creates resolves to undefined. Either delete this .then (in which case the previous .then will determine the value), or have it re-return the data.

.then(data => {
   console.log('Success:', data);
   return data;
})

The .catch block may require similar treatment. Any errors are currently being swallowed and replaced with resolving to undefined. If you want to re-throw the error, do one of the following:

.catch((error) => {
  console.error('Error:', error);
  throw error;
});

// or
.catch((error) => {
  console.error('Error:', error);
  return Promise.reject(error);
});
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