Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

javascript - 既然我们有ES6承诺,还有理由使用Q或BlueBird等承诺库吗? [关闭](Are there still reasons to use promise libraries like Q or BlueBird now that we have ES6 promises? [closed])

After Node.js added native support for promises, are there still reasons to use libraries like Q or BlueBird?(在Node.js添加了对promises的原生支持之后,还有理由使用像Q或BlueBird这样的库吗?)

For example if you are starting a new project and let's assume in this project you don't have any dependencies that use these libraries, can we say that there are really no more reasons to use such libraries?(例如,如果您正在开始一个新项目并让我们在这个项目中假设您没有任何使用这些库的依赖项,那么我们是否可以说实际上没有理由使用这些库?)   ask by Murat Ozgul translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The old adage goes that you should pick the right tool for the job.(古老的谚语说你应该为这份工作挑选合适的工具。)

ES6 promises provide the basics.(ES6承诺提供基础知识。) If all you ever want or need is the basics, then that should/could work just fine for you.(如果你想要或需要的只是基础知识,那么应该/可以为你做好。) But, there are more tools in the tool bin than just the basics and there are situations where those additional tools are very useful.(但是,工具箱中有更多工具而不仅仅是基础知识,并且在某些情况下,这些附加工具非常有用。) And, I'd argue that ES6 promises are even missing some of the basics like promisification that are useful in pretty much every node.js project.(而且,我认为ES6承诺甚至缺少一些像promisification这样的基础知识,这些基础知识在几乎每个node.js项目中都很有用。) I'm most familiar with the Bluebird promise library so I'll speak mostly from my experience with that library.(我对Bluebird promise库最熟悉,所以我主要根据我对该库的经验来讲。) So, here are my top 6 reasons to use a more capable Promise library(所以,这是我使用功能更强大的Promise库的六大理由) Non-Promisified async interfaces - .promisify() and .promisifyAll() are incredibly useful to handle all those async interfaces that still require plain callbacks and don't yet return promises - one line of code creates a promisified version of an entire interface.(非Promisified异步接口 - .promisify().promisifyAll()非常有用,可以处理所有那些仍然需要简单回调并且还没有返回promises的异步接口 - 一行代码创建整个接口的promisified版本。) Faster - Bluebird is significantly faster than native promises in most environments.(更快 - 在大多数环境中,Bluebird比本机承诺快得多 。) Sequencing of async array iteration - Promise.mapSeries() or Promise.reduce() allow you to iterate through an array, calling an async operation on each element, but sequencing the async operations so they happen one after another, not all at the same time.(异步数组迭代的排序 - Promise.mapSeries()Promise.reduce()允许您遍历数组,在每个元素上调用异步操作,但对异步操作进行排序,使它们一个接一个地发生,而不是全部在同一个时间。) You can do this either because the destination server requires it or because you need to pass one result to the next.(您可以这样做,因为目标服务器需要它,或者您需要将一个结果传递给下一个。) Polyfill - If you want to use promises in older versions of browser clients, you will need a polyfill anyway.(Polyfill - 如果您想在旧版浏览器客户端中使用promises,则无论如何都需要polyfill。) May as well get a capable polyfill.(也可以得到一个有能力的polyfill。) Since node.js has ES6 promises, you don't need a polyfill in node.js, but you may in a browser.(由于node.js具有ES6承诺,因此您不需要在node.js中使用polyfill,但您可以在浏览器中使用。) If you're coding both node.js server and client, it may be very useful to have the same promise library and features in both (easier to share code, context switch between environments, use common coding techniques for async code, etc...).(如果您正在对node.js服务器和客户端进行编码,那么在两者中使用相同的promise库和功能可能非常有用(更容易共享代码,环境之间的上下文切换,使用异步代码的常见编码技术等等)。 )。) Other Useful Features - Bluebird has Promise.map() , Promise.some() , Promise.any() , Promise.filter() , Promise.each() and Promise.props() all of which are occasionally handy.(其他有用的功能 - Bluebird有Promise.map()Promise.some()Promise.any()Promise.filter()Promise.each()Promise.props()所有这些偶尔都很方便。) While these operations can be performed with ES6 promises and additional code, Bluebird comes with these operations already pre-built and pre-tested so it's simpler and less code to use them.(虽然可以使用ES6承诺和附加代码执行这些操作,但Bluebird已经预先构建并预先测试了这些操作,因此使用它们更简单,代码更少。) Built in Warnings and Full Stack Traces - Bluebird has a number of built in warnings that alert you to issues that are probably wrong code or a bug.(内置警告和完整堆栈跟踪 - Bluebird有许多内置警告,提醒您可能出现错误代码或错误的问题。) For example, if you call a function that creates a new promise inside a .then() handler without returning that promise (to link it into the current promise chain), then in most cases, that is an accidental bug and Bluebird will give you a warning to that effect.(例如,如果你调用一个在.then()处理程序中创建新promise的函数而不返回该promise(将其链接到当前的promise链),那么在大多数情况下,这是一个偶然的错误,Bluebird会给你这是一个警告。) Other built-in Bluebird warnings are described here .(此处描述了其他内置Bluebird警告。) Here's some more detail on these various topics:(以下是有关这些主题的更多详细信息:) PromisifyAll(PromisifyAll) In any node.js project, I immediately use Bluebird everywhere because I use .promisifyAll() a lot on standard node.js modules like the fs module.(在任何node.js项目中,我立即使用Bluebird,因为我在标准node.js模块上使用.promisifyAll()很多,比如fs模块。) Node.js does not itself provide a promise interface to the built-in modules that do async IO like the fs module.(Node.js本身并不提供与fs模块一样执行异步IO的内置模块的promise接口。) So, if you want to use promises with those interfaces you are left to either hand code a promise wrapper around each module function you use or get a library that can do that for you or not use promises.(因此,如果你想在这些接口上使用promises,你可以手工编写你使用的每个模块函数的promise包装器,或者获得一个可以为你做这个或者不使用promises的库。) Bluebird's Promise.promisify() and Promise.promisifyAll() provide an automatic wrapping of node.js calling convention async APIs to return promises.(Bluebird的Promise.promisify()Promise.promisifyAll()提供了node.js的自动包装,调用约定async API来返回promises。) It's extremely useful and time saving.(这非常有用,节省时间。) I use it all the time.(我用它所有的时间。) Here's an example of how that works:(这是一个如何工作的例子:) const Promise = require('bluebird'); const fs = Promise.promisifyAll(require('fs')); fs.readFileAsync('somefile.text').then(function(data) { // do something with data here }); The alternative would be to manually create your own promise wrapper for each fs API you wanted to use:(另一种方法是为您想要使用的每个fs API手动创建自己的promise包装器:) const fs = require('fs'); function readFileAsync(file, options) { return new Promise(function(resolve, reject) { fs.readFile(file, options, function(err, data) { if (err) { reject(err); } else { resolve(data); } }); }); } readFileAsync('somefile.text').then(function(data) { // do something with data here }); And, you have to manually do this for each API function you want to use.(而且,您必须为要使用的每个API函数手动执行此操作。) This clearly doesn't make sense.(这显然没有意义。) It's boilerplate code.(这是样板代码。) You might as well get a utility that does this work for you.(您也可以使用一个实用程序来为您工作。) Bluebird's Promise.promisify() and Promise.promisifyAll() are such a utility.(Bluebird的Promise.promisify()Promise.promisifyAll()就是这样一个实用程序。) Other Useful Features(其他有用的功能) Here are some of the Bluebird features that I specifically find useful (there are a couple code examples below on how these can save code or speed development):(以下是我特别认为有用的一些Bluebird功能(下面有几个代码示例,介绍如何保存代码或加速开发):) Promise.promisify() Promise.promisifyAll() Promise.map() Promise.reduce() Promise.mapSeries() Promise.delay() In addition to its useful function, Promise.map() also supports a concurrency option that lets you specify how many operations should be allowed to be running at the same time which is particularly useful when you have a lot of something to do, but can't overwhelm some outside resource.(除了它的有用功能外, Promise.map()还支持一个并发选项,允许您指定允许同时运行的操作数,这在您有很多事情要做时特别有用,但是可以一些外部资源不堪重负。) Some of these can be both called stand-alone and used on a promise that itself resolves to an iterable which can save a lot of code.(其中一些既可以被称为独立的,也可以用于承诺,它本身可以解析为可以节省大量代码的迭代。) Polyfill(填充工具) In a browser project, since you generally want to still support some browsers that don't have Promise support, you end up needing a polyfill anyway.(在浏览器项目中,由于您通常仍希望支持某些没有Promise支持的浏览器,因此您最终还是需要一个polyfill。) If you're also using jQuery, you can sometimes just use the promise support built into jQuery (though it is painfully non-standard in some ways, perhaps fixed in jQuery 3.0), but if the project involves any signficant async activity, I find the extended features in Bluebird very useful.(如果你也在使用jQuery,你有时可以使用jQuery中内置的promise支持(尽管在某些方面很难非标准,可能在jQuery 3.0中修复),但是如果项目涉及任何重要的异步活动,我发现Bluebird中的扩展功能非常有用。) Faster(快点) Also worth noting that Bluebird's promises appear to be significantly faster than the promises built into V8.(另外值得注意的是,Bluebird的承诺似乎明显快于V8内置的承诺。) See this post for more discussion on that topic.(有关主题的更多讨论,请参阅<a href="https://stackoom.com/link/aHR0cHM6Ly9zb2Z0d2FyZWVuZ2luZWVyaW5nLnN0YWNrZXhjaGFuZ2UuY29tL3F1ZXN0aW9ucy8yNzg3Nzgvd2h5LWFyZS1uYXRpdmUtZXM2LXByb21pc2VzLXNsb3dlci1hbmQtbW9yZS1tZW1vcnktaW50ZW5zaXZlLXRoYW4tYmx1ZWJpcmQ=" rel="nofollow noopene

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...