bluebird

Sequential call of promises

混江龙づ霸主 提交于 2019-12-13 01:04:01
问题 I am having an issue with the order in which promises/functions get called. Basically I fetch an array of ID and then, for each id, want to fetch order detail and save. Then on to the next ID. As of now it will not save until every single order detail has been fetched. The code: // Convenience function var fetchOrders = function() { return self.fetchOrderList() .then(function(orders) { return P.map(orders, function(r) { return self.fetchOrderById(r.id).then(fn); }); }); }; // Fetch initial

Catching errors generated in Promises within Promises in JavaScript

狂风中的少年 提交于 2019-12-12 15:18:38
问题 Is it possible to have errors bubble up in Promises? See the code below for reference, I'd like to get promise1.catch to catch the error generated in promise2 (which current does not work with this code): function test() { var promise1 = new Promise(function(resolve1) { var promise2 = new Promise(function(resolve2) { throw new Error('Error in promise2!'); }); promise2.catch(function(error2) { console.log('Caught at error2', error2); }); }); promise1.catch(function(error1) { console.log(

Proper while() loop for bluebird promises (without recursion?)

那年仲夏 提交于 2019-12-12 13:08:10
问题 I've been learning promises using bluebird for two weeks now. I have them mostly understood, but I went to go solve a few related problems and it seems my knowledge has fell apart. I'm trying to do this simple code: var someGlobal = true; whilePromsie(function() { return someGlobal; }, function(result) { // possibly even use return value of 1st parm? // keep running this promise code return new Promise(....).then(....); }); as a concrete example: // This is some very contrived functionality,

Bluebird's Promise.settle doesn't resolve with the correct values

懵懂的女人 提交于 2019-12-12 12:26:28
问题 I have the following code: return Promise.settle(matches, imgur.uploadUrl) .map(function (inspection) { if (inspection.isFulfilled()) { return inspection.value().data.link; } return '#'; }) A more verbose version of the above displays the same problems: return Promise.settle(matches, function(match) { return imgur.uploadUrl(match); }) .then(function(results) { return results; }) .map(function (inspection) { if (inspection.isFulfilled()) { return inspection.value().data.link; } return '#'; })

Why do I not get the warning “a promise was created in a handler but was not returned from it”?

假装没事ソ 提交于 2019-12-12 09:49:30
问题 Edit: to be more clear - I'm NOT getting warning in example below. I would like to know why and see example code where warning would be printed. In my Node.js project I'm using bluebird Promises library. I observed some cases where I'm getting following warning: Warning: a promise was created in a handler but was not returned from it I understand that the problem occurs when there is promise that is not connected to any promise chain. I'm trying to fully understand how this works and what

Bluebird's Promise.all() method when one promise is dependent on another

泄露秘密 提交于 2019-12-12 09:39:37
问题 I'm writing some code that currently looks like this because I have dependencies in my code. I was wondering if there was a cleaner way to do this with Promise.all()? Here is my pseudo code: return someService.getUsername() .then(function(username) { user = username; }) .then(function() { return someService.getUserProps(user); }) .then(function(userProps) { userProperties = userProps; return someService.getUserFriends(user); }) .then(function(userFriends) { friends = userFriends; }) .catch

Sequential execution of Promise [duplicate]

孤者浪人 提交于 2019-12-12 04:11:25
问题 This question already has answers here : Aren't promises just callbacks? (8 answers) Closed 3 years ago . I have following promise functions implemented like return new Promise(function(resolve, reject) { //logic }); cart.getBasket(req) cart.updateBasket(req) cart.updateDefaultShipment(req) cart.getBasketObject(basket) Currently I execute code using app.post('/Billing', function(req, res) { cart.getBasket(req).then(function(basket) { cart.updateBasket(req).then(function() { cart

Bluebird.each break if solved

十年热恋 提交于 2019-12-12 03:57:15
问题 I want to test each element of an array until a condition is met then skip the rest. This is the code I came up with, it seems to be working but I'm not sure if its actually safe or it has unexpected side effects. Other solutions are welcomed. let buddyAdded = false; replicaArr = _.keys(ReplicaList); Promise.each(replicaArr, function(replicaname) { if (!buddyAdded) { Player.count({ 'buddyList': replicaname }, function(err, result) { if (err) { } else if (result < 990) { Player.update({ '_id':

rxjs subscription calls a function that returns a promise, need Promise.each style scheduling

﹥>﹥吖頭↗ 提交于 2019-12-12 03:36:34
问题 I'm trying to read JSON from a file, filter out stuff I don't want and possibly map and flatten the JSON elements, and then for each filtered JSON line in the file call a function that returns a promise. (Basically tail -f from a bunyan produced file). Additional Requirement: I want the pacing to be based on how fast the functions resolve the promise (output pacing, one element at a time, like Promise.each) Sounds like a great use for RXJS and promises, right? Well, except that all the

How to call Promise function in loop and save its return value

核能气质少年 提交于 2019-12-12 03:08:07
问题 I have created a promise function (using bluebird) called getBasketObject . This function expects a basket as an argument and than return a new basketObject out of it. basketObject has some variables like tax, total, shipping and productItems . Now, the productItems object has price, name, quantity properties available in it but it doesn't have productImageLink available. In order to get productImageLink I make a new async call to an endpoint which will get me the product images object. Image