bluebird

Need correct call to Promise reduce (when.reduce )

十年热恋 提交于 2020-01-05 07:21:26
问题 I have a processor function that takes a "cmd" object and returns a promise where the resolution is the same "cmd" object passed in (with a response key added). reduce here is when.reduce reduce = require('when').reduce; //return processor(cmds[0]) return reduce(cmds, function(processor, cmd) { Debug.L1('running processor for component ', cmd.component) return processor(cmd) }) .then(cmds => { Debug.L1('cmds with responses\n', cmds) let response = cmds.map(cmd => { return cmd.response })

promise cancellation still firing fulfill function

筅森魡賤 提交于 2020-01-04 16:03:13
问题 This is my code: var promiseResult = new BBpromise(function(resolve, reject){ console.log(1) // some actions setTimeout(function(){ resolve(); }, 2000); }).cancellable().catch(BBpromise.CancellationError, function(e){ console.log(e); }); promiseResult.then(function(){ console.log('AYYYY'); }); promiseResult.cancel(new BBpromise.CancellationError()).then(function(s){ console.log('canceled ?'); }); And I'm getting the following output: BTW.. the output seems to be immediate. Seems like if just

Nodejs bluebird promise fails while processing image

天涯浪子 提交于 2020-01-04 08:06:20
问题 //Created a promise for each image size. var promises = sizes.map(function (size) { return new Promise(function (resolve, reject) { var destinationDir = fileUtil.getAbsolutePathOfImage(destinationPath); fileUtil.createDirectoryIfNotExists(destinationDir); destinationDir += size.src; fileUtil.createDirectoryIfNotExists(destinationDir); //Resize the image. //console.log('imagefile : ' + JSON.stringify(imageFile)); //console.log('destinationDir: ' + JSON.stringify(destinationDir)); //Called an

Try and catch around promise

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 06:08:18
问题 I need to write a function that returns a promise, where first I call a synchronous function A() which returns some result. Then return a function B(result) where B is a promise which takes in the result of A(). If either function fails I want the same error function C(error) to get called where C is a promise. What is the best way of writing this. This is what I have but think there is obvious way I am missing function() { try { var result = A(); return B(result) .catch(function(error) {

Running promises in small concurrent batches (no more than X at a time)

若如初见. 提交于 2020-01-03 07:21:25
问题 The Async library has functions like eachLimit which can be used to efficiently spread a big batch of jobs over multiple CPU cores, like this: var numCPUs = require('os').cpus().length; var exec = require('child_process').exec; async.eachLimit(someArray, numCPUs, function (value, done) { exec('something --input' + value, done); }, finalCallback); This avoids overloading the system with too many commands at once, but still exploits multiple CPUs. I want to do the same thing but with Promises.

Bluebird promises and catch branching

帅比萌擦擦* 提交于 2020-01-03 04:51:04
问题 I'm wondering if there a way in Bluebird promises to .catch a thrown error and then process some specific actions without branching (nested promise). Say I have doSomethingAsync() .then(function (result) { if (!result) throw new CustomError('Blah Blah'); if (result == 0) throw new CustomError2('Blah Blah Blah'); return result; }) .then(function (result) { console.log('Success, great!'); }) .catch(CustomError, function (error) { // Oh CustomError! return saveSomethingAsync(); }) .then(function

Promise combine stream

对着背影说爱祢 提交于 2020-01-03 03:10:06
问题 I want read big file line by line and insert the data in DB store. My functon return a Promise inside it created stream, and resolve it when event stream.on('end') was invoked, but it is not I really want, because in stream.on('data') it produce Promise.map() on each line, and I want to be sure, that all insertion ops completed before resolve() invoked. How I can produce right chain in this case? var loadFromFile = (options) => new Promise(function (resolve, reject) { let stream = fs

Promise.settle and promise fulfillment vs rejection

人盡茶涼 提交于 2020-01-02 09:55:41
问题 Consider the following code that contains a simplified implementation of Bluebird's Promise.settle: var a = Promise.reject('a'); var b = Promise.resolve('b'); var c = Promise.resolve('c'); var promises = [a,b,c]; function settled(promises) { var alwaysFulfilled = promises.map(function (p) { return p.then( function onFulfilled(value) { return { state: 'fulfilled', value: value }; }, function onRejected(reason) { return { state: 'rejected', reason: reason }; } ); }); return Promise.all

Implementing a mix of Promise.all and Promise.settle

白昼怎懂夜的黑 提交于 2020-01-01 10:14:18
问题 I need to implement a version of Promise.all that would take an array of promises and return the result as it usually does, plus also settles all promises, much like Promise.settle does it within the Bluebird library, except I cannot use Bluebird , and have to rely just on the standard promise protocol. Would that be terribly complicated to implement? Or is it too much to ask here for an idea of how to implement it? I really hope not, so I'm asking, if anyone perhaps implemented it before, to

Is it safe to not resolve or reject a promise

南笙酒味 提交于 2020-01-01 09:07:11
问题 Imagine a web application with routes that need to check whether the user is allowed to access a given resource before proceeding. The "is authenticated" check relies on a database call. In each route, I may have: authorizeOwnership(req, res) .then(function() { // do stuff res.send(200, "Yay"); }); I want the authorizeOwnership() function to handle 403 (access denied) and 500 (e.g. database query error) responses so that each route doesn't need to do so explicitly. I have a function that can