index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. if (typeof process === 'undefined' ||
  3. !process.version ||
  4. process.version.indexOf('v0.') === 0 ||
  5. process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) {
  6. module.exports = { nextTick: nextTick };
  7. } else {
  8. module.exports = process
  9. }
  10. function nextTick(fn, arg1, arg2, arg3) {
  11. if (typeof fn !== 'function') {
  12. throw new TypeError('"callback" argument must be a function');
  13. }
  14. var len = arguments.length;
  15. var args, i;
  16. switch (len) {
  17. case 0:
  18. case 1:
  19. return process.nextTick(fn);
  20. case 2:
  21. return process.nextTick(function afterTickOne() {
  22. fn.call(null, arg1);
  23. });
  24. case 3:
  25. return process.nextTick(function afterTickTwo() {
  26. fn.call(null, arg1, arg2);
  27. });
  28. case 4:
  29. return process.nextTick(function afterTickThree() {
  30. fn.call(null, arg1, arg2, arg3);
  31. });
  32. default:
  33. args = new Array(len - 1);
  34. i = 0;
  35. while (i < args.length) {
  36. args[i++] = arguments[i];
  37. }
  38. return process.nextTick(function afterTick() {
  39. fn.apply(null, args);
  40. });
  41. }
  42. }