Skip to content

精读《async/await 是把双刃剑》 #82

Closed
@ascoders

Description

@ascoders
Owner

文章地址,前端的发展就是一个大逃亡,刚逃离了 callback 地狱,又要开始逃离 async/await 地狱。

Activity

atian25

atian25 commented on May 2, 2018

@atian25

@popomore 来,吐槽下。

Ariex

Ariex commented on May 7, 2018

@Ariex

代码写成这样,只能怪自己学艺不精吧?

a(() => {
  b();
});

c(() => {
  d();
});

等价的应该是

await Promise.all(a().then(b), c().then(d));

回调的问题在于,它必须是嵌套的。而async和await可以在一定程度上把异步代码写的像是同步的,从而达到更好的可读性。

ascoders

ascoders commented on May 7, 2018

@ascoders
OwnerAuthor

@Ariex 用 .then 确实会简化点

Ariex

Ariex commented on May 7, 2018

@Ariex

@ascoders 并不是用then代码能简化,而是作为例子里面的代码实在是没有按照async/await的要求来实现,如果随便写一些乱七八糟的代码就能拿来当作槽点的话,那就真是一念一地狱了。
我觉得文中以及原文中提到的这些问题,根本不是async/await的问题,而是写代码的人需要对自己使用的东西有正确的基础的了解。

ascoders

ascoders commented on May 7, 2018

@ascoders
OwnerAuthor

@Ariex 说的有一定道理,但是你能写好,其他人呢。?

希望能减少一些矫枉过正的评论,让其他读者可以平静的看到滥用带来的问题,不要陷入无谓的争斗。

fantix

fantix commented on May 7, 2018

@fantix

写代码的人需要对自己使用的东西有正确的基础的了解。

其他读者可以平静的看到滥用带来的问题,不要陷入无谓的争斗。

各一朵小红花。

https://python-gino.readthedocs.io/en/latest/why.html

还没写完,也还没来得及翻译,当做基础用于将来分享,请批评。

ae6623

ae6623 commented on May 7, 2018

@ae6623

@Ariex 对于一个刚开始接触async/await的前端小白来说,确实缺少一些成功的范例来引导《如何正确的使用async/await》,如果一个老手已经学会了轻车熟路,逃避async/await的陷阱,不妨来 一篇/几语 来指引一下。

atian25

atian25 commented on May 7, 2018

@atian25

@ascoders 我赞同 @Ariex 的观点,这跟不能跟 callback hell 并论的,后者是因为语言缺陷导致开发者容易陷入。但 async/await 的话,从文中举得例子来说,我觉得更多是开发者的问题,还有就是 Promise 的锅(逃。。。

ascoders

ascoders commented on May 7, 2018

@ascoders
OwnerAuthor

@atian25 文中的例子是有意而为的,因为 callback 和 async/await 都经常出现在业务代码中,而业务项目又经常在倒排,有时候项目室热起来,内心一急躁,就写出连珠炮的 await。。

atian25

atian25 commented on May 7, 2018

@atian25

还是举个稍微正常一点的例子吧,文中的 a b c 这样的太简陋了,如果能举一个带有含义的单词,并举出存在以下的场景,再来讨论会更有聚焦一点

a(() => {
  b();
});

c(() => {
  d();
});
baxtergu

baxtergu commented on May 7, 2018

@baxtergu

这篇文章的解读的前半部分使用到了英文原文的例子,后半部分采用了简化版的抽象例子。
我觉得英文原文里结合了生活实际的例子产生的歧义可能会小一点。
原文链接:https://medium.freecodecamp.org/avoiding-the-async-await-hell-c77a0fb71c4c

ascoders

ascoders commented on May 7, 2018

@ascoders
OwnerAuthor

用户详情页,根据用户 id 拿用户信息、历史文章列表、最近更新的文章列表;同时加载第三方问答插件,加载完毕后上报插件展现打点日志。

如果回调的方式写,是这样的:

getUser(123, user => {
  getArticles(user, articles => /* fresh view */)
  getRecentArticles(user, articles => /* fresh view */)
})

initQA(() => {
  reportQAPV()
})

换成 promise:

Promise.all([
  getUser().then(user => {
    getArticles(user).then(/* fresh view */)
    getRecentArticles(user).then(/* fresh view */)
  }),
  initQA().then(reportQAPV)
])

如果换成 async/await,最好拆两个函数:

async getUserInfo() {
  // 这里也要用到 .then,如果纯粹用 await 的话,这里代码会更多
  getArticles(user).then(/* fresh view */)
  getRecentArticles(user).then(/* fresh view */)
}
async initAndReportQA() {
  await initQA()
  reportQAPV()
}

async function main() {
  await getUserInfo()
  initAndReportQA()
}

如果写代码的时候比较急躁,可能会想在一个函数里快速完成任务:

const user = await getUser()
getArticles(user)
getArticles(user).then(/* fresh view */)

await initQA()
reportQAPV()

这样 initQA() 就滞后了。

ascoders

ascoders commented on May 7, 2018

@ascoders
OwnerAuthor

@xpal7512 如果用 callback 方式,就算写的急躁,顶多是不雅观,而不会出现性能问题。

23 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @netwjx@atian25@Ariex@fantix@baxtergu

        Issue actions

          精读《async/await 是把双刃剑》 · Issue #82 · ascoders/weekly