标签:new http 代码 ESS www resolve asc eject ISE
console.log(1);
setTimeout(function () {
	console.log(2);
	new Promise(function (resolve, reject) {
		console.log(3);
		resolve();
		console.log(4);
	}).then(function () {
		console.log(5);
	});
});
function fn() {
	console.log(6);
	setTimeout(function () {
		console.log(7);
	}, 50);
}
new Promise(function (resolve, reject) {
	console.log(8);
	resolve();
	console.log(9);
}).then(function () {
	console.log(10);
});
fn();
console.log(11);
// 以下代码需要在 node 环境中执行
process.nextTick(function () {
	console.log(12);
});
setImmediate(function () {
	console.log(13);
});
通过上面的介绍,我们就可以得出一个代码执行的优先级:
同步代码(宏任务) > process.nextTick > Promise(微任务)> setTimeout(fn)、setInterval(fn)(宏任务)> setImmediate(宏任务)> setTimeout(fn, time)、setInterval(fn, time),其中time>0
综上,最终的输出顺序是:1 8 9 6 11 12 10 2 3 4 5 13 7
详细的解说:http://www.laixiangran.cn/2018/04/16/JavaScript%E4%B9%8BEvent%20Loop/
标签:new http 代码 ESS www resolve asc eject ISE
原文地址:https://www.cnblogs.com/liliy-w/p/8989764.html