Node.js process.nextTick All In One
Node.js process.nextTick All In One
The Node.js
Event Loop
,Timers
, andprocess.nextTick()
// process.nextTick
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
https://nodejs.org/zh-cn/docs/guides/event-loop-timers-and-nexttick/
process.nextTick
vs setImmediate
const baz = () => console.log('baz');
const foo = () => console.log('foo');
const zoo = () => console.log('zoo');
const start = () => {
console.log('start');
setImmediate(baz);
new Promise((resolve, reject) => {
resolve('bar');
}).then((resolve) => {
console.log(resolve);
process.nextTick(zoo);
});
process.nextTick(foo);
};
start();
// start foo bar zoo baz
https://nodejs.dev/en/learn/understanding-setimmediate/
https://nodejs.dev/zh-cn/learn/understanding-processnexttick/
import { nextTick } from 'node:process';
console.log('start');
nextTick(() => {
console.log('nextTick callback');
});
console.log('scheduled');
// Output:
// start
// scheduled
// nextTick callback
https://nodejs.org/api/process.html#processnexttickcallback-args
queueMicrotask
() vs.process.nextTick
()
import { nextTick } from 'node:process';
Promise.resolve().then(() => console.log(2));
queueMicrotask(() => console.log(3));
nextTick(() => console.log(1));
// Output:
// 1
// 2
// 3
console.log('start');
queueMicrotask(() => {
console.log('microtask callback');
});
console.log('scheduled');
// Output:
// start
// scheduled
// microtask callback
https://nodejs.org/api/process.html#when-to-use-queuemicrotask-vs-processnexttick
queueMicrotask
let id = setInterval(() => {
console.log(`4`);
clearInterval(id);
});
setTimeout(() => {
console.log(`3`);
});
queueMicrotask(() => {
console.log(`2`);
});
console.log(`1`);
/*
1
2
3
4
*/
let id = setInterval(() => {
console.log(`5`);
clearInterval(id);
});
setTimeout(() => {
console.log(`4`);
});
Promise.resolve().then(() => console.log(`2`));
queueMicrotask(() => {
console.log(`3`);
});
console.log(`1`);
/*
1
2
3
4
5
*/
https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask
refs
process.nextTick
https://medium.com/@amanhimself/how-process-nexttick-works-in-node-js-cb327812e083
https://timnew.me/blog/2014/06/23/process-nexttick-implementation-in-browser/
https://www.geeksforgeeks.org/difference-between-process-nexttick-and-setimmediate-methods/
https://stackoverflow.com/questions/40629456/why-and-when-to-use-process-nexttick
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17166366.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2022-02-28 js comma operator All In One
2022-02-28 js Promise In Deep All In One
2021-02-28 Apple TV (6th) All In One
2021-02-28 macOS show wifi password All In One
2021-02-28 Apple Store 美国账号注册 All In One
2021-02-28 Apple TV All In One
2021-02-28 Apple 企业采购优惠政策 All In One