All Projects → ai → Nanodelay

ai / Nanodelay

Licence: mit
A tiny (25 bytes) Promise wrapper around setTimeout

Programming Languages

typescript
32286 projects

Nano Delay

A tiny Promise wrapper around setTimeout for JavaScript. Returns a Promise and resolve it after a specific amount of time.

  • Only 25 bytes (minified and gzipped), 10 times smaller than delay library.
  • Has good ES modules and TypeScript support.
import { delay } from 'nanodelay'

async function foo () {
  await delay(300)
  // Executed after 300 milliseconds
}

delay(300).then(() => {
  // Executed after 300 milliseconds
})
Sponsored by Evil Martians

Usage

The second argument will be used in Promise as resolved value (useful to pass variables between scopes):

createClient().then(client => {
  expect(client).toBeOK
  return delay(50, client)
}).then(client => {
  expect(client).toBeConnected()
})

For quick hacks you can load Nano Delay from CDN. Do not use it in production because of low performance.

import { delay } from 'https://cdn.jsdelivr.net/npm/nanodelay/index.js'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].