bluebird

Bluebird Promise.all - multiple promises completed aggregating success and rejections

孤街醉人 提交于 2019-12-17 18:22:11
问题 Someone brought up an interesting case today with bluebird, what is the best way to handle multiple promises where we're not interested in stopping on a given fulfillment or rejection but rather interested in inspecting the final result. An example: var p1 = new Promise(function(f,r){ setTimeout(function(){ console.log("p1"); f("yay"); }, 100); }); var p2 = new Promise(function(f,r){ setTimeout(function(){ console.log("p2"); r(new Error("boo")); }, 200); }) var p3 = new Promise(function(f,r){

How do I promisify the AWS JavaScript SDK?

主宰稳场 提交于 2019-12-17 18:12:22
问题 I want to use the aws-sdk in JavaScript using promises. Instead of the default callback style: dynamodb.getItem(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); I instead want to use a promise style: dynamoDb.putItemAsync(params).then(function(data) { console.log(data); // successful response }).catch(function(error) { console.log(err, err.stack); // an error occurred }); 回答1: I believe calls can now be

What is the equivalent of Bluebird Promise.finally in native ES6 promises? [duplicate]

半腔热情 提交于 2019-12-17 17:26:26
问题 This question already has answers here : ES6 promise settled callback? (7 answers) Closed 3 years ago . Bluebird offers a finally method that is being called whatever happens in your promise chain. I find it very handy for cleaning purposes (like unlocking a resource, hiding a loader, ...) Is there an equivalent in ES6 native promises? 回答1: As of February 7, 2018 Chrome 63+, Firefox 58+, and Opera 50+ support Promise.finally. In Node.js 8.1.4+ (V8 5.8+), the feature is available behind the

How does Bluebird's util.toFastProperties function make an object's properties “fast”?

北战南征 提交于 2019-12-17 17:23:34
问题 In Bluebird's util.js file, it has the following function: function toFastProperties(obj) { /*jshint -W027*/ function f() {} f.prototype = obj; ASSERT("%HasFastProperties", true, obj); return f; eval(obj); } For some reason, there's a statement after the return function, which I'm not sure why it's there. As well, it seems that it is deliberate, as the author had silenced the JSHint warning about this: Unreachable 'eval' after 'return'. (W027) What exactly does this function do? Does util

Promise.all in JavaScript: How to get resolve value for all promises?

不羁岁月 提交于 2019-12-17 09:55:32
问题 I wrote the following node.js file: var csv = require('csv-parser'); var fs = require('fs') var Promise = require('bluebird'); var filename = "devices.csv"; var devices; Promise.all(read_csv_file("devices.csv"), read_csv_file("bugs.csv")).then(function(result) { console.log(result); }); function read_csv_file(filename) { return new Promise(function (resolve, reject) { var result = [] fs.createReadStream(filename) .pipe(csv()) .on('data', function (data) { result.push(data) }).on('end',

Catching Errors in JavaScript Promises with a First Level try … catch

房东的猫 提交于 2019-12-17 06:43:34
问题 So, I want my first level catch to be the one that handles the error. Is there anyway to propagate my error up to that first catch? Reference code, not working (yet): Promise = require('./framework/libraries/bluebird.js'); function promise() { var promise = new Promise(function(resolve, reject) { throw('Oh no!'); }); promise.catch(function(error) { throw(error); }); } try { promise(); } // I WANT THIS CATCH TO CATCH THE ERROR THROWN IN THE PROMISE catch(error) { console.log('Caught!', error);

While loop using bluebird promises

旧巷老猫 提交于 2019-12-17 05:05:42
问题 I am trying to implement a while loop using promises. The method outlined here seems to work. http://blog.victorquinn.com/javascript-promise-while-loop it uses a function like this var Promise = require('bluebird'); var promiseWhile = function(condition, action) { var resolver = Promise.defer(); var loop = function() { if (!condition()) return resolver.resolve(); return Promise.cast(action()) .then(loop) .catch(resolver.reject); }; process.nextTick(loop); return resolver.promise; }; This

While loop using bluebird promises

℡╲_俬逩灬. 提交于 2019-12-17 05:05:02
问题 I am trying to implement a while loop using promises. The method outlined here seems to work. http://blog.victorquinn.com/javascript-promise-while-loop it uses a function like this var Promise = require('bluebird'); var promiseWhile = function(condition, action) { var resolver = Promise.defer(); var loop = function() { if (!condition()) return resolver.resolve(); return Promise.cast(action()) .then(loop) .catch(resolver.reject); }; process.nextTick(loop); return resolver.promise; }; This

Limit concurrency of promise being run

你离开我真会死。 提交于 2019-12-17 04:35:14
问题 I'm looking for a promise function wrapper that can limit / throttle when a given promise is running so that only a set number of that promise is running at a given time. In the case below delayPromise should never run concurrently, they should all run one at a time in a first-come-first-serve order. import Promise from 'bluebird' function _delayPromise (seconds, str) { console.log(str) return Promise.delay(seconds) } let delayPromise = limitConcurrency(_delayPromise, 1) async function a() {

How do I use Bluebird with Angular?

主宰稳场 提交于 2019-12-17 04:19:05
问题 I tried using Angular with Bluebird promises: HTML: <body ng-app="HelloApp"> <div ng-controller="HomeController">{{name}} {{also}}</div> </body> JS: // javascript var app = angular.module('HelloApp', []); app.controller("HomeController", function ($scope) { var p = Promise.delay(1000).then(function () { $scope.name = "Bluebird!"; console.log("Here!", $scope.name); }).then(function () { $scope.also = "Promises"; }); $scope.name = "$q"; $scope.also = "promises"; }); window.app = app; [Fiddle]