current-tick
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

current-tick

A tiny modern no-nosence "current-tick" implementation that equeues your function to be executed later within the current event loop cycle, exactly what unfortunately named process.nextTick 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.

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.

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.

const asap = currentTick ||
  typeof setImmediate === 'function'
    ? setImmediate
    : fn => setTimeout(fn, 0);

asap function is already prepared for you:

import asap from 'current-tick/lib/asap';
 
asap(() => console.log('world!'));
console.log('Hello');
// 👉 Hello world!

License

Unlicense — public domain.

Readme

Keywords

none

Package Sidebar

Install

npm i current-tick

Weekly Downloads

2

Version

1.0.0

License

Unlicense

Unpacked Size

7.21 kB

Total Files

14

Last publish

Collaborators

  • streamich