简体   繁体   English

我应该在ES6节点项目中使用多少Promise?

[英]How much should I be using promises in ES6 node projects?

In the official bluebird promises page it is written that if you are using node.js it's very unlikely I will have to write promises myself. 在官方的bluebird promises页面中,如果您使用的是node.js,那么我不太可能必须自己编写promises。

Since I started on a new project I found all my code base revolving around promises. 自从我开始一个新项目以来,我发现我所有的代码库都围绕promise。 For example I have a databaseConnector that returns a promise, an express route that takes a promise, tests with chai-as-promised that test promises and in general I haven't written any function that receives a callback. 例如,我有一个返回promise的databaseConnector,一个接受promise的快速路由,使用chai-as-promise测试该promise,并且通常我没有编写任何接收回调的函数。

Should I be writing callback modules and if needed promisify them? 我应该编写回调模块吗?如果需要的话,可以使用它们?

What are the cons if any? 有什么缺点?

Should I be using promises in ES6 node projects? 我应该在ES6节点项目中使用Promise吗?

Yes, definitively. 是的,绝对可以。 Promises are the new standard asynchronous interface. 承诺是新的标准异步接口。

In the official bluebird promises page it is written that it's very unlikely I will have to write promises myself. 在官方的蓝鸟承诺页面中写道,我不太可能自己写承诺。

Not exactly. 不完全是。 What they mean here is that you hardly ever will need to use the new Promise constructor function - most of its usages are an antipattern . 它们在这里的意思是您几乎不需要使用new Promise构造函数-大多数用法都是反模式
You will want to use promises everywhere , but you don't want to create them explicitly from callbacks. 您将希望在任何地方都使用诺言 ,但是您不想从回调中显式创建它们。 If you have asynchronous code that takes callbacks, promisification is much easier to use than new Promise . 如果您有采用回调的异步代码,则promisificationnew Promise易于使用。

Since I started on a new project I found all my code base revolving around promises 自从我开始一个新项目以来,我发现我所有的代码库都围绕promise

You're lucky! 你真幸运! All the functions you are working with already return and expect promises - that's great! 您正在使用的所有功能都已经返回并有望实现承诺-太好了! You can just use them, embrace them. 您可以使用它们,拥抱它们。 You don't have to worry about odd callback patterns in your promise code. 您不必担心您的promise代码中的奇怪的回调模式。

Should I be writing callback modules and if needed promisify them? 我应该编写回调模块吗?如果需要的话,可以使用它们?

No. Especially not if you all the APIs you're working with already use promises. 不会。尤其是如果您使用的所有API都已使用promises,则不是这样。 Promises make much simpler and more correct code. 承诺使代码更简单,更正确。 They're just great . 他们很棒

Comparing to callbacks , you should almost always use promises . 回调相比,您几乎应该始终使用promise They are way more readable, solve some of the nesting problems and provide a standardized way to notify on errors (using reject() ). 它们更具可读性,可以解决一些嵌套问题,并提供一种标准化的方式来通知错误(使用reject() )。

What the official bluebird promises page could mean is 官方蓝鸟承诺页面可能意味着

  • on node, you can often solve things more performant using streams (like gulp does), where you often have callbacks instead of promises (think of es.map() ) 在节点上,您通常可以使用流来解决性能更高的问题(如gulp一样),在这种情况下,您通常具有回调而不是promise( es.map()
  • as you mentioned es6 , you could use generators instead of callbacks/promises. 正如您提到的es6一样 ,您可以使用生成器来代替回调/承诺。 They don't work well together, so stick with one or the other, but they are handy in some cases. 它们不能很好地协同工作,因此彼此坚持,但是在某些情况下它们很方便。
  • you could even use the es7 feature async/await , which will hopefully make everything else obsolete in the future. 您甚至可以使用es7功能async / await ,希望将来会淘汰其他所有功能。

Cons of callbacks. 回调的缺点。 Well... They once were continuations and no compiler designer even thought of bothering language users directly with this. 好吧……它们曾经是延续 ,没有编译器设计师甚至想到直接用这种语言打扰语言用户。 Then node.js came and now everyone writes endlessly nested function calls that are not readable (don't even think of debugging), don't provide ways for error handling (except the de-facto standard of having err as the first parameter for the callback) and don't interact well with synchonous code. 然后node.js出现了,现在每个人都写了不可读的 无休止的嵌套函数调用(甚至不考虑调试),不提供错误处理方式 (事实上​​,将err作为第一个参数的标准)回调),并且不能与同步代码很好地交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM