bunkat / later

A javascript library for defining recurring schedules and calculating future (or past) occurrences for them. Includes support for using English phrases and Cron schedules. Works in Node and in the browser.
http://bunkat.github.io/later/
MIT License
2.42k stars 244 forks source link

Implementing on Async function to EndPoint(Ajax) #225

Open evicent opened 6 years ago

evicent commented 6 years ago

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!!

happydenn commented 6 years ago

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