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

135
Vistas
Resolve an Await function if it takes more than x seconds in Javascript / Node.js

I have an await function() which waits for an external API. Usually, these APIs take 600ms to return data. But sometimes it's taking around 10 seconds. So how do I write a function so that if it takes more than 1 second, then break that function and resolve anyway (don’t wait for the API)

Here's the main function code which calls the API.

console.log("Before Await");

await fetchExternalAPI("data");

console.log("Continue if success or takes more than x seconds");

The actual function:

const fetch = require('node-fetch');

module.exports = async function fetchExternalAPI(data) {

    try {
        const response = await fetch("https://www.example.com/api/log", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(data)
        });

        const returnData = await response.json();
        console.log("Response: " + JSON.stringify(returnData))
        return returnData
    }
    catch (e) {
        console.log("Error: " + e)
        return e
    } 
}

I want to return success fo the above function

  1. if fetch is success
  2. or if exceeded more than x seconds

Both should return success no matter the real output.

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

0

You can use AbortController to abort the fetch method, inside a setTimeout with 1sec, and if the fetch resolved before the setTimeout callback execute, then clearTimeout

const fetch = require('node-fetch');

module.exports = async function fetchExternalAPI(data) {

    try {
        let controller = new AbortController();
        let timeId = setTimeout(() => {
           controller.abort()
 
        }, 1000)
        const response = await fetch("https://www.example.com/api/log", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(data)
            signal: controller.signal
        });
        clearTimeout(timeId)
        const returnData = await response.json();
        console.log("Response: " + JSON.stringify(returnData))
        return returnData
    }
    catch (e) {
        console.log("Error: " + e)
        return e
    } 
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you need to use Axios.

npm i axios 

than in body you have one property timeout.

const instance = await axios.create({
 baseURL: 'https://some-domain.com/api/',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'}
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add a promise in your call to resolve on time and reject if too long. The following code has not been tested but that's the idea

  const fetch = require('node-fetch');

  module.exports = async function fetchExternalAPI(data) {

    try {
      const response = await fetch("https://www.example.com/api/log", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data)
      });

      await new Promise((resolve, reject) => {
        setTimeout(reject, 50000));
        resolve();
      }).then(() => {
        const returnData = await response.json();
        console.log("Response: " + JSON.stringify(returnData))
        return returnData
      }).catch(() => {
        return 'Took too long';
      }));
    }
    catch (e) {
      console.log("Error: " + e)
      return e
    } 
  }
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