Like after
, but for the world of promises. Resolve a promise after some number of calls.
This depends on an environment with Proxies.
npm i -S @substrate-system/after
This exposes ESM and common JS via package.json exports
field.
import { after } from '@substrate-system/after'
const after = require('@substrate-system/after')
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.
cp ./node_modules/@substrate-system/after/dist/index.min.js ./public/after.min.js
<script type="module" src="./after.min.js"></script>
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...')
})
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