如何在Node 4.x中使用Promises? [英] How to use Promises in Node 4.x?

查看:46
本文介绍了如何在Node 4.x中使用Promises?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,ES6是Node 4.x中的正式标准,您如何使用Promises?模块(例如本机 fs 模块)是否可以自动与Promises一起使用?还是必须专门编写模块才能与Promises一起使用?您如何知道可以与什么一起使用Promise,哪些不能使用?

Now that ES6 is officially standard in Node 4.x, how do you use Promises? Do modules (for example the native fs module) automatically work with Promises? Or modules have to be written specifically to work with Promises? How do you know what you can use a Promise with and what you can't?

有太多不同的Promise实现(Q,Bluebird等),我对从Node 4.x中的本机Promises开始的位置感到困惑.

There have been so many different Promise implementations (Q, Bluebird, etc) that I am confused on where to start with native Promises in Node 4.x.

推荐答案

您如何使用Promises?

how do you use Promises?

您可以通过使用新的构造函数Promise使用本机" promise.可在MDN上中找到该API的基础知识,并且可以在HTML5Rocks上找到对Promises >的深入研究.

You can use "native" promises by using the new constructor, Promise. The basics of the API can be found over on MDN, and an in-depth study of Promises can be found on HTML5Rocks.

模块(例如本机fs模块)是否可以自动与Promises一起使用?

Do modules (for example the native fs module) automatically work with Promises?

他们没有,但是这并不困难做一个包装纸.当然,对整个Node API进行此操作可能会有些乏味.

They do not, but it's not so difficult to make a wrapper. Of course, doing this for the whole Node API could be a bit tedious.

OP中的一条评论提到了Bluebird的 PromisifyAll ,这似乎是一种将整个模块转换为Promises的神奇方法.好漂亮!

One of the comments in the OP mentioned Bluebird's PromisifyAll, which seems like a magic way to convert an entire module to use Promises. Nifty!

我希望在以后的Node版本中使用一些Promise认证的API,但是我不确定.围绕此问题可能存在问题或讨论……也许是在io.js项目上.如果发现任何问题,我将进行调查和更新.

I would expect some Promise-ified APIs in coming versions of Node, but I'm not certain of this. There might be an issue or discussion somewhere around this...perhaps on the io.js project. I'll investigate and update this if I find anything.

至于为什么我们有Promises 一个回调样式的API?这可能是因为,添加,添加Promises比进行可能的破坏API更改(例如删除回调)要容易得多.

As for why we have Promises and a callback-style API? That's likely because of the fact that it's easier to be additive, adding Promises, than it is to make possibly breaking API changes, like removing callbacks.

还是必须专门编写模块才能与Promises一起使用?

Or modules have to be written specifically to work with Promises?

他们这样做.

您怎么知道可以与什么一起使用Promise,什么不能使用?

How do you know what you can use a Promise with and what you can't?

文档始终是提出API问题的最佳场所.我不知道会返回Promises的任何东西,但是我承认我每天都使用Node API的一小部分.

The documentation is always the best place to go for API questions. I don't know of anything that returns Promises, but I admittedly use a small subset of the Node API in my day-to-day.

有太多不同的Promise实现(Q,Bluebird等),我对从Node 4.x中的本机Promises开始的位置感到困惑.

There have been so many different Promise implementations (Q, Bluebird, etc) that I am confused on where to start with native Promises in Node 4.x.

您对这些不同的实现有任何具体问题吗?我认为它的方式是原生Promise很小.它们足以使异步代码更简洁一些,但是没有多余的装饰.您提到的其他库有两件事:

Do you have any specific questions about these different implementations? The way I think about it is that native Promises are pretty minimal. They're good enough to make asynchronous code a little bit cleaner, but they have no frills. Those other libraries you mention do two things:

  1. 为不支持本机Promises的环境提供Polyfill
  2. 添加不同程度的精美功能

Node中不再需要第一个用例.因此,要确定您是否要/需要其中的任何功能.

The first use case is no longer necessary in Node. So it's a matter of determining if you want/need any of those fancy features.

让我知道这个答案是否令人满意,我将尝试用更多信息来更新它!

Let me know if this answer isn't satisfactory, and I'll try to update it with more info!

这篇关于如何在Node 4.x中使用Promises?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆