in development
Small functions that can be used to handle timing in async functions in JavaScript.
Note:
- Assumes support for at least es2015 (needs
Promise
and arrow functions). Please transpile and/or add polyfills when appropriate. - The timing will never be precise as it depends on functions like setTimeout that do not provide precise timing.
- Keep in mind that these functions are never blocking.
These functions are meant to be used within async functions (es2017).
They can be used inside the browser (main thread and worker) or NodeJS.
There is no default import, all these helper functions are available as named exports.
import { sleep } from 'timing-functions';
async function message() {
console.log("it' gonna be legend...");
await sleep(200); // Waits for approximately 200ms
console.log('...wait for it...');
await sleep(1000); // Waits for approximately 1s
console.log('...dary!');
}
message();
A TypeScript declaration file is available and published alongside the code.
The code is bundled and published as CommonJS and ES modules.
Waits for approximately the number of ms provided.
name | default value | accepted values | information | required |
---|---|---|---|---|
time |
none | number | time to wait in ms | yes |
Waits for the next animationFrame.
none
Tries to wait for an idle time.
name | default value | accepted values | information | required |
---|---|---|---|---|
maxTimeout |
none | number | maximum time to wait in before continuing | no |
Provides a way to timeout a Promise
. If the Promise
you passed takes longer
than the timout to resolve, it will reject.
name | default value | accepted values | information | required |
---|---|---|---|---|
timeout |
none | number | time to wait in ms before timing out | yes |
promise |
none | Promise | Promise that will be wrapped |
yes |
message |
none | string | message that will be passed when rejecting | no |