import { interval } from "https://deno.land/x/delayed@2.0.2/mod.ts";
Return an asynchronous generator that yield indefinitely the callback at least after the specified delay
Examples
for await (const result of interval(fetch, 60_000, {}, 'https://api.example.com')) {
console.log(result)
}
//log some api result every minutes
const idRef = [1] //Use array to keep reference of the variable
function fetchUser([id]: number) {
return fetch(`https://jsonplaceholder.typicode.com/users/${id}`)
}
for await (const response of interval(fetchUser, 500, {}, idRef)) {
if (!response.ok) break
const user = await response.json()
console.log(user, idRef[0]++)
}
//print one of all 10 users every 500ms until get 404
const ac = new AbortController()
let index = 0
for await (const user of interval(getUser, 500, ac, index++)) {
user.delete()
}
//ac called elsewhere, eg: callback
ac.abort()
//abort interval before deleting all users