-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
nextTick.js
45 lines (38 loc) · 885 Bytes
/
nextTick.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Next tick for both node and browser.
*
* |Name|Desc |
* |----|----------------|
* |cb |Function to call|
*
* Use process.nextTick if available.
*
* Otherwise setImmediate or setTimeout is used as fallback.
*/
/* example
* nextTick(function() {
* // Do something...
* });
*/
/* module
* env: all
*/
/* typescript
* export declare function nextTick(cb: types.AnyFn): void;
*/
_('types');
if (typeof process === 'object' && process.nextTick && !LICIA_TEST) {
exports = process.nextTick;
} else if (typeof setImmediate === 'function') {
exports = function(cb) {
setImmediate(ensureCallable(cb));
};
} else {
exports = function(cb) {
setTimeout(ensureCallable(cb), 0);
};
}
function ensureCallable(fn) {
if (typeof fn !== 'function')
throw new TypeError(fn + ' is not a function');
return fn;
}