线程的案例题

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);
});

  综上,最终的输出顺序是: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/

猜你喜欢

转载自www.cnblogs.com/liliy-w/p/8989764.html