https://github.com/substrate-system/after
Resolve a promise after n calls
https://github.com/substrate-system/after
after promise
Last synced: 10 months ago
JSON representation
Resolve a promise after n calls
- Host: GitHub
- URL: https://github.com/substrate-system/after
- Owner: substrate-system
- License: other
- Created: 2024-12-23T02:41:16.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-22T05:46:23.000Z (about 1 year ago)
- Last Synced: 2025-04-14T11:03:17.222Z (about 1 year ago)
- Topics: after, promise
- Language: TypeScript
- Homepage: https://substrate-system.github.io/after/
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# after

[](README.md)
[](README.md)
[](https://semver.org/)
[](./CHANGELOG.md)
[](https://packagephobia.com/result?p=@substrate-system/after)
[](package.json)
[](LICENSE)
Like [`after`](https://github.com/Raynos/after), but for the world of promises. Resolve a promise after some number of calls.
This depends on an environment with [Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy).
Contents
- [install](#install)
- [Module Format](#module-format)
* [ESM](#esm)
* [Common JS](#common-js)
* [pre-built JS](#pre-built-js)
- [Example](#example)
* [async](#async)
## install
```sh
npm i -S @substrate-system/after
```
## Module Format
This exposes ESM and common JS via [package.json `exports` field](https://nodejs.org/api/packages.html#exports).
### ESM
```js
import { after } from '@substrate-system/after'
```
### Common JS
```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
```sh
cp ./node_modules/@substrate-system/after/dist/index.min.js ./public/after.min.js
```
#### HTML
```html
```
## 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.
```js
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
```js
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
```