Giter VIP home page Giter VIP logo

Comments (2)

metcoder95 avatar metcoder95 commented on June 19, 2024

👋
The issue is rooted to the way your code is scheduling the retry within the timeout for retrying once more.
The first setTimeout has the correct statements for handling a situation of a failure in case of an error, but on the second timeout called within the finally scope, it just calls the loadPosition once more without any error handling; meaning that if it fails, it will be treated as an unhandled rejection leading to the process termnation.

A possible alternative can be:

async function loadPosition () {
  const response = await fetch('https://api.open-notify.org/iss-now.json')
  const data = await response.json()

  if (!response.ok || !data.iss_position || data.message !== 'success') {
    throw new Error('Invalid response')
  }

  const { latitude, longitude } = data.iss_position || {}

  return { latitude, longitude }
}

const callback = async () => {
  try {
    const { latitude, longitude } = await loadPosition()
    console.log(`The ISS is at ${latitude}/${longitude}!`)
  } catch (error) {
    console.error('Could not fetch ISS position data')
  } finally {
    setTimeout(callback, 5000)
  }
}

setTimeout(callback, 5000)

It produces:

Could not fetch ISS position data
Could not fetch ISS position data
Could not fetch ISS position data

from undici.

Radiergummi avatar Radiergummi commented on June 19, 2024

Ohh, this is embarrassing. Of course. Thanks a bunch.

from undici.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.