Skip to content

Instantly share code, notes, and snippets.

@hunghg255
Created October 10, 2024 07:51
Show Gist options
  • Save hunghg255/d68e5bb41bee94d92cb468395dae8920 to your computer and use it in GitHub Desktop.
Save hunghg255/d68e5bb41bee94d92cb468395dae8920 to your computer and use it in GitHub Desktop.
nextTick nodejs
const getDataService = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('data');
}, 1000);
});
};
const getA = async () => {
process.nextTick(async () => {
// do something
console.log('start3 getA');
});
const data = await getDataService();
console.log(data);
return data;
};
async function start() {
// await getA();
process.nextTick(async () => {
// do something
console.log('start nextTick');
console.log('end nextTick');
});
await getA();
console.log(1);
setTimeout(() => {
console.log(3);
} , 0);
Promise.resolve().then(() => {
console.log(2);
});
}
start();
// - Trước process.nextTick không có tác vụ dạng async, block I/IO, thứ tự chạy
// callstack => microtask queue => process.nextTick => macrotask queue
// - Trước process.nextTick có tác vụ dạng async, block I/IO, thứ tự chạy
// callstack => process.nextTick => microtask queue => macrotask queue
// - nextTick + normal + async
// normal => nextTick => async => microtask queue => macrotask queue
// - nextTick + async + normal
// nextTick => async => normal => microtask queue => macrotask queue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment