3.0.2 • Published 4 years ago
w2t v3.0.2
w2t
Tiny asynchronous action timer. 150 bytes gzipped.
Install
npm i w2t --saveWhat is this?
Sometimes UI actions feel more human if we delay them instead of allowing them to
be completed at computer time. w2t allows you to specify actions and a minimum
time for them to be completed before the Promise is resolved.
If your specified action(s) take longer than the minimum, w2t will wait for
all actions to complete before resolving.
Usage
w2t is a thin wrapper around Promise.all, so the API is essentially the
same: resolved values are returned on an array, with order preserved.
Below, w2t will resolve foo and baz, but wait 500 ms before returning.
import wait from 'w2t'
const foo = async () => 'bar'
const baz = async () => 'qux'
wait(500, [ foo, baz ]).then(([ resolvedFoo, resolvedBaz ]) => {
console.log(resolvedFoo, resolveBaz) // 'bar qux'
})If no actions are provided, w2t becomes useful as a "sleep" function.
// do something
await wait(600)
// do something 600ms laterChangelog
v2.0.0- Typescript rewrite, add testsv1.0.0- initial release
License
MIT License © Eric Bailey