Giter Site home page Giter Site logo

nexttick's Introduction

nextTick

Common functions using process.nextTick()

Installation

npm install nexttick

Usage

var nextTick = require('nexttick');

nextTick(function () {
  console.log('Hello, world');
});

API

nextTick( fn [, arg1, arg2, ...] )

nextTick(function () { console.log('hello'); });

nextTick(function (a, b) {
  console.log(a, b); // foo bar
}, 'foo', 'bar');

nextTick.loop( fn(exit) [, times] )

// specified loop length
var i = 0;
nextTick.loop(function () {
  console.log(++i);
}, 50);

// with exit
var i = 0;
nextTick.loop(function (exit) {
  console.log(++i);
  if (i > 100) exit();
});

nextTick.while( truth, fn (exit) )

var i = 0;
nextTick.while(function () { // this function is evaluated on each iteration
  return i < 100;
}, function () {
  console.log(++i);
});

nextTick.forEach( array, fn (value, index, array, exit) )

nextTick.forEach([ 1, 2, 3 ], function (value, index, array, exit) {
  console.log(index, value);
});

nextTick.slice( array [, begin [, end]] ).then( fn (sliced) )

nextTick.slice([ 1, 2, 3, 4, 5 ], 2, -1).then(function (sliced) {
  console.log(sliced); // [ 3, 4 ]
});

nextTick.in( hash, fn (value, key, hash, exit) )

nextTick.in({ foo: 'bar', baz: 'zoo' }, function (value, key, hash, exit) {
  console.log(key, value);
});

All loop methods accept a callback using a chained .then(cb) method. You may also pass any arguments you want to it using the exit([arg1, arg2, ...]) function.

nextTick.forEach([ 1, 2, 3 ], function (value) {
  console.log(value);
}).then(function () { // this will be called when the above finishes
  console.log('All done');
});

Error handling

Simply pass an Error to the exit() function:

nextTick.forEach([ 1, 2, 3 ], function (value, index, array, exit) {
  exit(new Error('Some error'));
}).then(function (err) {
  if (err) throw err;
});

nexttick's People

Contributors

stagas avatar tofu9989 avatar

Watchers

 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.