GithubHelp home page GithubHelp logo

streamich / current-tick Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 2.11 MB

Enqueue function to execute later within current event loop cycle

License: The Unlicense

JavaScript 23.86% TypeScript 76.14%

current-tick's Introduction

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 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.

current-tick's People

Contributors

dependabot[bot] avatar semantic-release-bot avatar streamich avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.