bluebird

Co.js and bluebird.js — what's the difference?

梦想与她 提交于 2020-01-01 05:16:31
问题 Could someone help me understand the differences between using Koa.js and Bluebird.js with ES6 Harmony. Specifically, how does co( function * () { //stuff } ); compare to, Promise.coroutine( function * () { //stuff } ); It just seems Koa should be using Bluebird and not recreating the wheel. What's different? 回答1: For now the difference is that Koa allows yielding more than just promises. However a feature is being added that allows not only yielding callbacks, thunks etc but any arbitrary

How to iterate an array synchronously using lodash, underscore or bluebird [closed]

☆樱花仙子☆ 提交于 2020-01-01 04:47:05
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I have an array that contains filenames at each index. I want to download those files one at a time (synchronously). I know about ' Async ' module. But I want to know whether any functions in Lodash or Underscore or Bluebird library supports this functionality. 回答1: You can use bluebird's Promise

Are nested catches within promises required?

守給你的承諾、 提交于 2020-01-01 03:55:06
问题 We would like to reduce the number of catch blocks inside our promises. If we remove the nested catches, will exceptions bubble up to the parent catch? temporaryUserModel.findOne({email: req.body.email}) .then(tempUser => { if (tempUser) { temporaryUserModel.findOneAndUpdate({_id: tempUser.toJSON()._id}, user) .then((doc) => { return res.status(200).json({ status: 'Success', data: {url: planOpted.chargifySignupUrl} }); }) .catch(err => error(err, res)); } else { temporaryUserModel(user).save(

Promises and streams using Bluebird.js and Twitter stream

别来无恙 提交于 2020-01-01 03:17:08
问题 I am new super new to Promises and Node and am curious about using promises with streams. Can I promisify a stream? Using Bluebirdjs and the Twit module I have the following: var Twit = require('twit') var Promise = require("bluebird"); var T = new Twit({ consumer_key: process.env.CONSUMER_KEY, consumer_secret: process.env.CONSUMER_SECRET, access_token: process.env.ACCESS_TOKEN, access_token_secret: process.env.ACCESS_TOKEN_SECRET }) Promise.promisifyAll(Twit); Promise.promisifyAll(T); var

Can I do a “lazy” promise with Bluebird.js? [closed]

断了今生、忘了曾经 提交于 2019-12-30 16:51:13
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I want a promise that waits until then is called before it runs. That is, if I never actually call then , the promise will never run. Is this possible? 回答1: Make a function which creates and returns a promise on first invocation, but returns the same promise on each subsequent invocation: function

Can I break a chain early with bluebird Promises?

北战南征 提交于 2019-12-30 08:32:12
问题 I don't necessarily want to error, but I have: getFromDb().then (tradeData) -> if not tradeData # DO NOT CONTINUE THE CHAIN else getLatestPrice tradeData .then (latestPrice) -> ... .then -> ... .then -> ... .catch (err) -> next err Any way for me to abort the chain if there is no tradeData? 回答1: getFromDb().then (tradeData) -> if tradeData getLatestPrice tradeData -> .then (latestPrice) -> ... .then -> ... .then -> ... .catch (err) -> next err else getSomethingElse -> send("no data") In 3.0,

Managing promise dependencies

廉价感情. 提交于 2019-12-30 06:17:48
问题 I'm using Node.js and Bluebird to create some fairly complicated logic involving uncompressing a structured file, parsing JSON, creating and making changes to several MongoDB documents, and writing related files in multiple locations. I also have fairly complicated error handling for all of this depending on the state of the system when an error occurs. I am having difficulty thinking of a good way to manage dependencies through the flow of promises. My existing code basically looks like this

How to extract data out of a Promise

随声附和 提交于 2019-12-30 01:59:09
问题 I have a promise that returns data and I want to save that in variables. Is this impossible in JavaScript because of the async nature and do I need to use onResolve as a callback? Can I somehow use this (e.g. wrap it with async/await): const { foo, bar } = Promise.then(result => result.data, errorHandler); // rest of script instead of this? Promise.then(result => { const { foo, bar } = result.data; // rest of script }, errorHandler); Note: Bluebird library is used instead of native

Catch Error Type in Bluebird Not Working

徘徊边缘 提交于 2019-12-29 02:09:33
问题 I have a custom error class: class NetworkError extends Error { constructor() { super('Network Error'); this.name = 'NetworkError'; } } And I want to handle it specifically: import {NetworkError} from '../../common/errors'; someFunc().catch(NetworkError, err => { // this is missed }).catch(err => { // this is hit }); But it's skipping my custom catch and hitting the general catch. If I change it like so, it works: someFunc().catch({name: 'NetworkError'}, err => { // this is hit }).catch(err =

How to run after all javascript ES6 Promises are resolved

主宰稳场 提交于 2019-12-29 00:06:09
问题 I'm in the process of replacing some old code that used jQuery Deferred objects and I am rewriting using Bluebird/ES6 Promises. If I have multiple asynchronous calls, how can I trigger a function after all the promises are resolved. Using jQuery Deferreds it would be something like this: var requests = [...]; //some arbitrary data that is iterated to generate multiple ajax requests var promises = []; resuests.forEach(function(endpoint) { promises.push($.ajax({url: endpoint})); }); $.when