-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JS基础] Promise #4
Comments
由于 JavaScript 的单线程特性,函数执行具有原子性,称为完整运行(run-to-completion)特性 任务队列 (job queue),挂在事件循环队列的每个 tick 之后的一个队列。在事件循环的每个 tick 中,可能出现的异步动作不会导致一个完整的新事件添加到事件循环队列中,而会在当前 tick 的任务队列末尾添加一个项目(一个任务)。 Excuse me?这个前端面试在搞事! let a = 0;
Promise.resolve().then(() => {
console.log(`Promise: ${a}`);
return 2;
}).then((v) => {
console.log(`Promise: ${v}`);
});
a++;
setTimeout(() => {
a++;
console.log(`setTime: ${a}`);
}, 0);
/*
Promise: 1
Promise: 2
setTime: 2
*/ |
回调函数存在的问题: Promise 是如何解决的?
// 不会出现同步异步混用,导致竞态条件
getUserByName('nolan').then(function (user) {
if (inMemoryCache[user.id]) {
return inMemoryCache[user.id]; // returning a synchronous value!
}
return getUserAccountById(user.id); // returning a promise!
}).then(function (userAccount) {
// I got a user account!
}); 信任问题:
|
Promise 的异常处理
|
macrotask和microtask microtasks:
macrotasks:
macrotasks 具有先后顺序,每个 macrotasks 执行完成后会将当前 microtasks 都执行完 |
Async/Await 的坑 |
参考:
你不知道的JavaScript(中卷)- Promise 部分
We have a problem with promises
The text was updated successfully, but these errors were encountered: