为什么NodeJS不将Promise用于readFile API? [英] Why does NodeJS NOT use Promise for the readFile API?

查看:149
本文介绍了为什么NodeJS不将Promise用于readFile API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这本书 https://pragprog.com/book/tbajs/async-javascript,我发现了这一点:

In the book https://pragprog.com/book/tbajs/async-javascript, I found this:

Node的早期迭代在其非阻塞API中使用了Promises.然而, 2010年2月,瑞安·达尔(Ryan Dahl)决定改用 现在熟悉的callback(err,results ...)格式,理由是 承诺是属于用户区"的更高层次的结构.

Node’s early iterations used Promises in its nonblocking API. However, in February 2010, Ryan Dahl made the decision to switch to the now-familiar callback(err, results...) format, on the grounds that Promises are a higher-level construct that belongs in "userland."

这让我感到困惑,因为作为读取文件的API,这

It looks quite confusing to me, because as an API to read files, this

fs.readFile('/etc/passwd')
.onSuccess(function(data){console.log(data)})
.onError(function(err){throw err})

看起来比这好得多

fs.readFile('/etc/passwd', function (err, data) {
  if (err) throw err;
  console.log(data);
});

有人对承诺是一个更高层次的构造"为什么会阻止自己在NodeJS API中使用的想法有想法吗?

Does anyone have ideas about why "Promises are a higher-level construct" will stops itself from being used in NodeJS API?

推荐答案

Node v8附带了将回调API转换为Promise的util.promisify,Node v10附带了本机Promise支持(实验性):

Node v8 ships with util.promisify that converts callback APIs to promises, Node v10 ships with native promises support (experimental):

const fs = require('fs').promises;

// in an async function:
let data = await fs.readFile('/etc/passwd');
console.log(data);


未来是承诺:

NodeJS 对新的API使用承诺.实际上,目前正在讨论如何.多年前,由于摩擦和性能问题,在0.2中更早地尝试使用Promises失败了.


The future is promises:

NodeJS will use promises for the new APIs. In fact it is currently discussed how. An earlier attempt in 0.2 to use Promises in node years ago failed because of friction and performance issues.

现在的诺言是一种本地语言功能,但是在将其实现为核心API之前,必须先进行以下事情:

Now promises are a native language feature, but the following has to happen before they make it to the core APIs:

  • 承诺必须是一种本地语言构造,这已经发生了.
  • 最近宣布的NodeJS和io.js合并必须发生-时间范围可能是几个月.
  • v8(JavaScript引擎)团队必须完成私有符号的工作,这将使快速的Promise创建成为可能.目前,promise构造函数是在本地promise中创建promise的唯一方法,并且它分配了一个相对昂贵的闭包. Domenic目前正在与io.js和v8团队紧密合作,以确保正确完成此工作.
  • v8团队必须优化promise的实现,当前的原生promise始终如bluebird这样的用户区实现会丢失.这也正在发生.
  • Promises have to be a native language construct this already happened.
  • The NodeJS and io.js merger that was recently announced has to happen - the time frame is a few short months probably.
  • The v8 (JavaScript engine) team has to finish working on private symbols which will enable fast promise creation. At the moment the promise constructor is the only way to create promises in native promises and it allocates a closure which is relatively expensive. This is currently being done with Domenic working in tight coordination between the io.js and v8 team to ensure this is done properly.
  • The v8 team has to optimize the promise implementation, currently native promises lose consistently to userland implementations like bluebird. This is also happening now.

一旦发生所有这些情况,该API将被派生,并且包含Promise的版本将被集成到核心中. 这是一个漫长而无趣的讨论-在这方面有更好的讨论io.js/NG回购,但两者都不是真正有用的信息.

Once all these happen the API will be forked and a version containing promises will be integrated into core. Here is a long and uninteresting discussion about it - there is a better one at the io.js/NG repo but neither are really too informative.

的问题之类的库为您提供了可立即使用的工具以快速有效的方式将回调API转换为promises .您今天就可以使用它们并获得该功能.

Libraries like bluebird give you tools to instantly convert a callback API to promises in a fast and efficient way. You can use them today and get that functionality.

这篇关于为什么NodeJS不将Promise用于readFile API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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