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

[node] 浅谈node异步编程 #9

Open
zhangjin-007 opened this issue Apr 18, 2018 · 1 comment
Open

[node] 浅谈node异步编程 #9

zhangjin-007 opened this issue Apr 18, 2018 · 1 comment

Comments

@zhangjin-007
Copy link
Owner

zhangjin-007 commented Apr 18, 2018

异步编程的痛点

还记得刚开始写前端的时候,如果我们按照顺序执行,牺牲代码质量,比如模块化,代码可重用,还有可维护,下面列举一个例子,这个例子看完之后有种似曾相似的感觉
0f644176-7ca9-4706-af1f-14bd3afce59a
这个问题叫做callback hell

callback discipline

有哪些规约可以减少上面的痛点呢

  • 你需要及时退出程序使用 return continue break ,而不是使用if/else
  • 命名callback函数成外面的clousure,传递参数给callback,
    命名好我们的函数会对栈追踪起到更好的效果(stack trace)
  • 我们需要模块化我们的代码,使其可用,切分更小
    重构后的代码

5ad3fd07-ff07-45ca-a22c-6748fc9e0860

fc935663-c337-4878-b604-16866306e85a

d47096da-a72d-41d9-ad1e-b202c4296b10

c1eba384-9043-498e-a906-ed62165ea315

@zhangjin-007
Copy link
Owner Author

未完待续,太懒了,先截图凑合吧

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

No branches or pull requests

1 participant