later icon indicating copy to clipboard operation
later copied to clipboard

Implementing on Async function to EndPoint(Ajax)

Open evicent opened this issue 8 years ago • 1 comments

Hi!. Im trying to implement a later.setInterval function, and the function is an async call to API (Ajax). Is there any reestriccion? The thing is that the function is executed just once. I change the api function using a 'console.log' function, and worked properly...any comments?

Regards!!

evicent avatar Feb 23 '18 19:02 evicent

@evicent It seems like later can work with an async function just fine. The following is a simple example grabbing a random Star Wars character every 5 seconds using later.setInterval

const later = require('later')
const axios = require('axios')

const s = later.parse.text('every 5 seconds')

later.setInterval(async () => {
  const randomIndex = Math.ceil(Math.random() * 20)
  const { data } = await axios.get(`https://swapi.co/api/people/${randomIndex}/`)

  console.log(data)
}, s)

Maybe you can provide a simple example on what isn't working properly for you?

happydenn avatar Apr 19 '18 02:04 happydenn