5 ms·
Bluebird in the wild: Advanced promise-based workflows
- diegorbaquero 10y agoGreat article. I'm still using callbacks >.< The before code should not have Promise: "const fs = Promise.promisifyAll(require('fs'))"
- hiphipjorge 10y agoThanks diego! Yes, you're totally right. That line is not needed. I'll go in and fix it. But because of how `promisifyAll` works the code would still work the same way :)
- untog 10y agoOne thing I was surprised to discover is that promisifyAll alters the original object - which is actually great for ES6 imports. So: import fs from 'fs'; Promise.promisifyAll(fs); works great.
- altano 10y agoThat probably won't work when you switch from Babel=>ES5 to Babel=>ES6 (http://www.2ality.com/2015/07/es6-module-exports.html?m=1 http://www.2ality.com/2015/07/es6-module-exports.html?m=1) as ES6 bindings are immutable bindings to the actual values in the module instead of copies.
- romanovcode 10y agoThere is nothing wrong in using callbacks. I actually see many developers over-use promises to the point where code is nearly unreadable.
- wrong_variable 10y agoCallbacks are fine, if you treat them as monads and only use them for notification purposes. In liveScript do error,output <- fs.readFile "helloWorld.txt" console.log output.toString() # => "hello world from txt file" you can combine that with co-routines to create really powerful abstractions, without the massive memory footprint of promises.
- adrusi 10y agoTreating callbacks as monads is literally the definition of promises. If that code does the same thing it did in 2011 when I last looked at livescript, that's not treating callbacks as monads, thats just borrowing a syntax that Haskell uses for monads and using it for callbacks.
- spriggan3 10y ago> In liveScript i.e. use a different language because callbacks in javascript suck ? I prefer promises thank you. And promises ARE monads. No need for liveScript.
- karn09 10y agoGood read. I've been using bluebird in my own code lately and love it.
- jwalton 10y agoIf you're looking for a more lightweight solution than Bluebird (because, say, you're writing client side code - Bluebird is a bit heavy weight): * [promise-tools](https://github.com/benbria/node-promise-tools https://github.com/benbria/node-promise-tools) is a library with a lot of the cool flow control from Bluebird, but is tiny and works with native Promises. * [promise-breaker](https://github.com/jwalton/node-promise-breaker https://github.com/jwalton/node-promise-breaker) is a library I wrote which is intended to make it really easy for third party libraries to support both promises and callbacks with a minimum of fuss (we also use this extensively internally as we convert our giant code base from callbacks to promises.) `callFn` and `applyFn` are also very handy for calling into existing callback based libraries from within promise based libraries.
- vkjv 10y agoVery neat! If you are interested in high level promises without any dependencies, like these, I have a small library inspired by some of the more useful helpers in Bluebird: [extends-promise](https://github.com/kjvalencik/extends-promise https://github.com/kjvalencik/extends-promise) Fair warning, it has an emphasis on simplicity of code rather than speed. It does a few things that are very likely inefficient. I wrote it primarily to scratch an itch I had--embedding in one-off node.js scripts without any dependencies.
- untog 10y agoAh, this was exactly what I was going to ask, thanks! Promise tools looks great - do you know of any equivalents of promisify and promisifyAll? I use them a lot with native libraries. EDIT: to answer my own question, this looks like it'll do it: https://github.com/pgaubatz/node-es6-promisify-all https://github.com/pgaubatz/node-es6-promisify-all but it doesn't seem to be actively maintained.
- mofle 10y agoCheck out https://github.com/sindresorhus/pify https://github.com/sindresorhus/pify
- amelius 10y ago
- danesparza 10y agoQuick question: With ES6/ES2015 based native promises on the way (see https://babeljs.io/docs/learn-es2015/ https://babeljs.io/docs/learn-es2015/ ), why would you still want to use something relatively proprietary like Bluebird?
- creullin 10y agoGood reasoning here. Bluebird also has alot of utility methods that native promises don't have/support. http://programmers.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than-bluebird http://programmers.stackexchange.com/questions/278778/why-ar...
- deleted 10y ago[deleted]
- hiphipjorge 10y agoOP here. Bluebird provides a lot of really useful methods and abstractions over promises that we use on a daily basis. Basically all the methods I point out in the article are not part of the Promise spec (and probably won't be). The other thing that Bluebird helps a lot with is providing functionality around transitioning from non-promise code to promises by providing methods such as `promisify`, `promisifyAll`, `fromCallback`, and `asCallback`.
- _greim_ 10y agoBluebird made a name for itself by really, really focusing on V8-specific optimizations. Also it has added a lot of features that go beyond the promise spec, thus becoming one of those bandwagony swiss army knife sort of libraries. I personally use it for how efficient it is in Node.
- Touche 10y ago"proprietary" is an odd way to describe a MIT licensed project.
- onestone 10y agoBecause: 1) Bluebird is orders of magnitude faster and more memory-efficient than the current V8 native promises. Better native promises performance has been promised, but we are still waiting for it. 2) Bluebird is just as much standard-compliant as are the native promises. 3) Bluebird has very useful extensions, which are not (yet?) part of the standard. You are not forced to use them, but they are still very nice to have.
- joeyrobert 10y agoGood article but no mention of Promise.coroutine [1] which I find unfortunate. It allows for a synchronous looking code using generators and yield. I've been using it on express servers (in lieu of Koa) and it's worked great with promise-based ORMs (BookshelfJS). Also allows for use of native try/catch. [1] http://bluebirdjs.com/docs/api/promise.coroutine.html http://bluebirdjs.com/docs/api/promise.coroutine.html
- farnsworth 10y agoCool, like async/await in Typescript and which I think is coming in ES7.
- tonyle 10y agoWhen I first got into promises, they were annoying for certain tasks. Something as simple as download a list of 1000 files was hard to do since you didn't want all the calls to fire off at once. After a long journey with various techniques, I found out Bluebird map had a concurrency parameter. return Promise.map(arrayOfUrlsToDownload,downloadAndParseFunction,{concurrency:1}) One line for all your basic scraping/parsing needs.
- DonPellegrino 10y agoThere's also [Bottleneck](https://github.com/SGrondin/bottleneck https://github.com/SGrondin/bottleneck) for more fine grained concurrency.