Naughty Programmer's Madness
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

macrotask

3.0.0 • Public • Published

Macrotask Build Status

npm install macrotask

macrotask is a macrotask library based on immediate, and is almost identical except that immediate is a microtask, the difference as stated by Dominic here

The setImmediate API, as specified, gives you access to the environment's task queue, sometimes known as its "macrotask" queue. This is crucially different from the microtask queue used by web features such as MutationObserver, language features such as promises and Object.observe, and Node.js features such as process.nextTick. Each go-around of the macrotask queue yields back to the event loop once all queued tasks have been processed, even if the macrotask itself queued more macrotasks. Whereas, the microtask queue will continue executing any queued microtasks until it is exhausted.

In practice, what this means is that if you call setImmediate inside of another task queued with setImmediate, you will yield back to the event loop and any I/O or rendering tasks that need to take place between those calls, instead of executing the queued task as soon as possible.

The standard as it eventually got codified was that macrotasks were supposed to only get a single task from the queue per cycle instead of emptying the queue and cycling event loop before calling any recursive tasks.

This library ends up splitting the difference by tacking advantage of requestIdleCallback when available to run one or more tasks from the queue depending on how long they take, for environments that don't have requestIdleCallback we give ourselves a time limit of 10ms after which we will cycle the event loop before executing more tasks. At least one task will always be executed per loop.

npm install --save macrotask

provides 2 methods, run and clear, they work just like setImmediate does where run enqueues a task

const task = macrotask.run(function (a, b) {
  console.log(a, b);
}, 'hello', 'there');
// prints hello there

and clear cancels it

const task = macrotask.run(function (a, b) {
  console.log(a, b);
}, 'hello', 'there');
macrotask.clear(task);
// does not print anything

you can use it via a number of different methods:

// commonjs style in node or browserify
const macrotask = require('macrotask');
 
// es6 style with rollup or babel
import macrotask from 'macrotask';
// or
import {run, clear} from 'macrotask';

or you can just drop the macrotask.js or macrotask.min.js scripts from the root directory of the repo.

Keywords

none

install

npm i macrotask

Downloadsweekly downloads

1

version

3.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability