https://github.com/streamich/current-tick
Enqueue function to execute later within current event loop cycle
https://github.com/streamich/current-tick
Last synced: 11 months ago
JSON representation
Enqueue function to execute later within current event loop cycle
- Host: GitHub
- URL: https://github.com/streamich/current-tick
- Owner: streamich
- License: unlicense
- Created: 2018-12-08T10:34:46.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T08:47:19.000Z (over 2 years ago)
- Last Synced: 2025-07-22T15:55:18.362Z (11 months ago)
- Language: TypeScript
- Homepage:
- Size: 2.06 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# current-tick
A tiny modern no-nonsense "current-tick" implementation that equeues your function to be executed
later __within the current event loop cycle__, exactly what unfortunately named
[`process.nextTick`](https://nodejs.org/api/process.html#process_process_nexttick_callback_args) does in Node.js,
but `current-tick` also works in the browser.
It will try below methods in the following order:
1. `process.nextTick`
2. `Promise`
3. `MutationObserver`
4. `WebkitMutationObserver`
`current-tick` will be equal to `null` if your evironment does not support any of the above methods.
```js
import currentTick from 'current-tick';
if (!currentTick) {
console.log('Your environment is not supported!');
}
currentTick(() => console.log('world!'));
console.log('Hello');
// 👉 Hello world!
```
If you use TypeScript, because `currentTick` may be `null`, if you know for sure your evironment supports
at least one of the methods, you can use `!` to tell the TypeScript compiler that `currentTick` is not `null`.
```ts
currentTick!(fn);
```
Alternatively, you can define an `asap` method, that will 100% always exist, but
__will not guarantee that your function is always executed within the current event loop cycle__.
```js
const asap = currentTick ||
typeof setImmediate === 'function'
? setImmediate
: fn => setTimeout(fn, 0);
```
`asap` function is already prepared for you:
```js
import asap from 'current-tick/lib/asap';
asap(() => console.log('world!'));
console.log('Hello');
// 👉 Hello world!
```
## License
[Unlicense](LICENSE) — public domain.