main.js (1586B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var proc = require( 'process' ); 24 25 26 // MAIN // 27 28 /** 29 * Adds a callback to the "next tick queue". 30 * 31 * ## Notes 32 * 33 * - The queue is fully drained after the current operation on the JavaScript stack runs to completion and before the event loop is allowed to continue. 34 * 35 * @param {Callback} clbk - callback 36 * @param {...*} [args] - arguments to provide to the callback upon invocation 37 * 38 * @example 39 * function beep() { 40 * console.log( 'boop' ); 41 * } 42 * 43 * nextTick( beep ); 44 */ 45 function nextTick( clbk ) { 46 var args; 47 var i; 48 49 args = []; 50 for ( i = 1; i < arguments.length; i++ ) { 51 args.push( arguments[ i ] ); 52 } 53 proc.nextTick( wrapper ); 54 55 /** 56 * Callback wrapper. 57 * 58 * ## Notes 59 * 60 * - The ability to provide additional arguments was added in Node.js v1.8.1. The wrapper provides support for earlier Node.js versions. 61 * 62 * @private 63 */ 64 function wrapper() { 65 clbk.apply( null, args ); 66 } 67 } 68 69 70 // EXPORTS // 71 72 module.exports = nextTick;