Skip to content

Commit

Permalink
fix: next-tick iife
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed Jul 10, 2022
1 parent 05fa80a commit 218cc8c
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/function/next-tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@ import { ensureCallable } from '../debug';

import type { AnyFn } from '../type';

function _nextTick(): (callback: Function, ...args: any[]) => void {
/**
* 延迟执行
* event loop
* @category Function
*/
function nextTick(callback: Function, ...args: any[]) {
// nodejs
if (isFunction(typeof process === 'object' && process && process.nextTick)) {
return process.nextTick;
return process.nextTick(ensureCallable<AnyFn>(callback), ...args);
}

if (isFunction(typeof queueMicrotask == 'function' && queueMicrotask)) {
return function (cb) {
queueMicrotask(ensureCallable(cb));
};
if (typeof queueMicrotask == 'function') {
return queueMicrotask(ensureCallable<AnyFn>(callback));
}

if (isFunction(typeof setImmediate === 'function' && setImmediate)) {
return function (cb, ...args) {
setImmediate(ensureCallable<AnyFn>(cb), ...args);
};
if (typeof setImmediate === 'function') {
return setImmediate(ensureCallable<AnyFn>(callback), ...args);
}

return function (cb, ...args) {
setTimeout(ensureCallable(cb), 0, ...args);
};
return setTimeout(ensureCallable<AnyFn>(callback), 0, ...args);
}

/**
* 延迟执行
* event loop
* @category Function
*/
const nextTick = _nextTick();

export default nextTick;

0 comments on commit 218cc8c

Please sign in to comment.