Skip to content
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

手写一个假冒伪劣Promise会不会被喷? #7

Open
lizhongzhen11 opened this issue Jun 29, 2018 · 0 comments
Open

手写一个假冒伪劣Promise会不会被喷? #7

lizhongzhen11 opened this issue Jun 29, 2018 · 0 comments
Labels
js基础 Good for newcomers

Comments

@lizhongzhen11
Copy link
Owner

lizhongzhen11 commented Jun 29, 2018

myPromise

2019-12-12修改

时隔一年多,回顾头来自己再写一次,巩固下,发现同样的this丢失问题依然困扰了我一会,基础还是不够牢固。

最重要的是,我发现先我以前模拟的Promise居然是错的!!!

then 方法内部用的居然是 同步操作。。。OMG,我一直没意识到,直到今天回顾才发现,卧槽,为何人家 then 里面用 setTimeout???

再结合经典面试题

new Promise(resolve => {
  console.log(1)
  resolve(2)
}).then(res => console.log(res))
console.log(3)

// 打印输出是 1 3 2

then 里面可不就是个微任务嘛。。。(但是setTimeout是宏任务,网上大多用它来实现Promise。但其实两者是有区别的!)
PS:js里面有宏任务和微任务之分,vue源码中的nextTick也涉及这方面知识。可以看Tasks, microtasks, queues and schedules(宏任务与微任务)了解。

宏任务:

微任务:

尼玛,真尴尬啊!!!

果断把以前的内容给删了,这么尴尬的事情怎么能留下来呢!

阅读

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js基础 Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant