0.0.4 • Published 7 months ago
@substrate-system/after v0.0.4
after
Like after
, but for the world of promises. Resolve a promise after some number of calls.
This depends on an environment with Proxies.
install
npm i -S @substrate-system/after
Module Format
This exposes ESM and common JS via package.json exports
field.
ESM
import { after } from '@substrate-system/after'
Common JS
const after = require('@substrate-system/after')
pre-built JS
This package exposes minified JS files too. Copy them to a location that is accessible to your web server, then link to them in HTML.
copy
cp ./node_modules/@substrate-system/after/dist/index.min.js ./public/after.min.js
HTML
<script type="module" src="./after.min.js"></script>
Example
Resolve a promise after three function calls.
The value returned, next
, is a function and also a promise. Call it when you want to increment the count.
import { after } from '@substrate-system/after'
const next = after(3)
// 1
setTimeout(() => next(), 10)
// 2
setTimeout(() => next(), 20)
// 3
setTimeout(() => next(), 30)
// 4 -- this does nothing
setTimeout(() => next(), 40)
next.then(() => {
console.log('30ms later...')
})
async
const next = after(3)
setTimeout(() => next, 100)
setTimeout(() => next, 200)
setTimeout(() => next, 300)
const time = Number(new Date())
await next // wait for 3 calls -- 300ms
const afterTime = Number(new Date())
const diff = afterTime - time
console.log(diff >= 300) // true