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 ' ) ;
nextTick.loop( fn(exit) [, times] )
var i = 0 ;
nextTick . loop ( function ( ) {
console . log ( ++ i ) ;
} , 50 ) ;
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 ( ) {
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 ) ;
} ) ;
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 ( ) {
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 ;
} ) ;