Skip to main content
The Deno 2 Release Candidate is here
Learn more

Delayed

Tags deno doc License: MIT

Delayed is a simple module that provide an asynchronous approach to interval, delay and timeout.

Usage

All modules are exposed in mod.ts

See documentation here.

Examples

import * as Delayed from 'https://deno.land/x/delayed/mod.ts'

await Delay.sleep(500) //sleep 500ms
Delay.sleepSync(500) //synchronous sleep of 500ms

Asynchronous interval and timeout

import { timeout, interval } from 'https://deno.land/x/delayed/mod.ts'

/**
 * Timeout
*/
const { status } = await timeout(() => fetch('https://deno.land'), 500) //wait 500ms and then status === 200

/**
 * Interval
*/
const idRef = [1]

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