bluebird

Creating node library that support both node callbacks and promise with bluebird

做~自己de王妃 提交于 2019-12-07 13:56:33
问题 I am creating a node module and I want to be able to support both node callback and Promise APIs. The library that I hear the best things about (mainly that is it the fastest) is bluebird. So after reading some docs and looking at some other libraries that are using bluebird, I thought this would be the cleanest way to get a method to support both node callback and Promise APIs: this.isAllowed = function(role, resource, permission, callback) { var isAllowedAsync = bluebird.promisify(isAllowed

Promise has no method 'sort'

霸气de小男生 提交于 2019-12-07 13:24:22
问题 I am using Mongoose with Bluebird and am hitting an Error when using a query that includes a sort on a time stamp. I am trying to retrieve only the most recent entry. The query works when using the built in Promises. Any ideas? Thanks! var Promise = require("bluebird"), mongoose = require('mongoose'); var Item = Promise.promisifyAll(mongoose.model("Item")); Promise.promisifyAll(Item.prototype); var connect = function () { var options = { server: { socketOptions: { keepAlive: 1 } } }; var

Bluebird Promise Cancellation

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 13:11:46
问题 Say I have the following Promise chain: var parentPromise = Promise.resolve() .then(function () { var condition = false; if (condition) { return parentPromise.cancel('valid reason'); } else { return Promise.resolve() .then(function () { var someOtherCondition = true; if (someOtherCondition) { console.log('inner cancellation'); return parentPromise.cancel('invalid reason'); } }); } }) .catch(Promise.CancellationError, function (err) { console.log('throwing'); if (err.message !== 'valid reason'

adding promises to an array of promises in a for loop [duplicate]

泄露秘密 提交于 2019-12-07 12:12:52
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . let's assume the following example: var bb = require('bluebird'); var promiseStack = []; var deferred = bb.defer(); promiseStack.push(deferred.promise); bb.delay(2500).then(function() { deferred.resolve(); }); bb.all(promiseStack).then(function() { console.log('done'); }); Why isn't it possible to do the following: var bb = require('bluebird'); var promiseStack

Get states of tasks ran by bluebird.all().spread()

老子叫甜甜 提交于 2019-12-06 14:26:17
问题 I have two tasks ran by Bluebird: // Require bluebird... var Promise = require("bluebird"); // Run two tasks together Promise .all([Git.getRemotes(), GitFtp.getFtpRemotes()]) .spread(function (remotes, ftpRemotes) { // Something cool }); With q.js I had as response: remotes.value (the response of my task) remotes.state ("fullfilled" or "rejected" depending if the task thrown an error or not) ftpRemotes.value ftpRemotes.state So inside the spread() part I was able to check the state of each

Resolve *and* reject all promises in Bluebird

本小妞迷上赌 提交于 2019-12-06 10:25:26
I'm looking for the way to get both resolutions and rejections from promise array. I'm currently counting on Bluebird implementation, so ES6 compatible solution would be suitable, too. The best thing that comes to mind is to use Bluebird's Promise.settle for that, and I consider promise inspections an unnecessary complication here: let promises = [ Promise.resolve('resolved'), Promise.resolve('resolved'), Promise.reject('rejected') ]; // is there an existing way to do this? let resolvedAndRejected = Promise.settle(promises) .then((inspections) => { let resolved = []; let rejected = [];

What to use instead of Promise.all() when you want all results regardless of any rejections

半城伤御伤魂 提交于 2019-12-06 09:32:02
I'm using the Bluebird promise library in a node.js project. I have two operations that both return promises and I want to know when both are done, whether resolved or rejected and I need the return values from both. I'm reading the contents of multiple files and some of the files may not exist and that's an OK condition, so though fs.readFileAsync() will fail if the file doesn't exist, I still need the results of the other read operation. Promise.all(p1, p2) will reject if either p1 or p2 rejects and I don't think I'll necessarily get the data from the other one. Of all the other Bluebird

Convert async code to promise

自古美人都是妖i 提交于 2019-12-06 08:16:02
I use the following code and I need to convert it to promise and at the end return object which contain the file configuration, how should I do that ? var Promise = require('bluebird'), glob = promisifyAll(require("glob")), fs = Promise.promisifyAll(require("fs")); module.exports = { parse: function (configErr) { glob("folder/*.json", function (err, files) { if (err) { return configErr(new Error("Error to read json files: " + err)); } files.forEach(function (file) { fs.readFileAsync(file, 'utf8', function (err, data) { // Read each file if (err) { return configErr(new Error("Error to read

Promise.settle and promise fulfillment vs rejection

末鹿安然 提交于 2019-12-06 07:54:02
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(alwaysFulfilled); } //Update status message once all requests finish settled(promises).then(function (outcomes)

Nodejs exec mongodb command in Bluebird Promise

倖福魔咒の 提交于 2019-12-06 07:03:09
I want to run mongod command in node.js using Promises, so that database operations can run only after mongodb process is started. I tried my hands with following, but failed: var Promise = require("bluebird"); var execAsync = Promise.promisify(require('child_process').exec); execAsync("~/mongodb/bin/mongod").then(function(result){ console.log("started mongodb..."); }).catch(function(error){ console.log("error in starting mongodb..."+JSON.stringify(error)); }); Any suggestions? You shouldn't start your mongod process in node, you should be doing it some other way. Then you can just check