bluebird

Setting a timeout for each promise within a promise.all

只愿长相守 提交于 2020-01-12 19:11:30
问题 I am able to successfully perform a Promise.all, and gracefully handle resolves and rejects. However, some promises complete within a few milliseconds, some can/could take a while. I want to be able to set a timeout for each promise within the Promise.all, so it can attempt to take a maximum of say 5seconds. getData() { var that = this; var tableUrls = ['http://table-one.com','http://table-two.com']; var spoonUrls = ['http://spoon-one.com','http://spoon-two.com']; var tablePromises = that

Stop running processes after a Promise is rejected

偶尔善良 提交于 2020-01-12 04:26:47
问题 I'm using the following code which working OK, but the problem is that when I get an error, I want it to stops all the other promises. For example if chi.getCommand(val1, val2) , will send a reject and I got to the exception catch, I want to cancel the promises for chss.exe and app.getStatus(12); How can I achieve that? var start = Promise.all([ chi.getCommand(val1, val2), chi.findAndUpdateCustomer() ]).spread(function (command, customer) { return chss.exe(runnableDoc, command, customer)

Wait promise inside for loop

不打扰是莪最后的温柔 提交于 2020-01-09 05:23:07
问题 let currentProduct; for (let i = 0; i < products.length; i++) { currentProduct = products[i]; subscription.getAll(products[i]._id) .then((subs) => { update(subs, currentProduct); }); } I'm using bluebird, the methods getAll and update return promises. How can I say "Wait until the two promises return, then update the currentProduct value"? I'm quite new to JS... 回答1: This will be straightforward if you can use async / await : // Make sure that this code is inside a function declared using //

How to promisify?

寵の児 提交于 2020-01-06 19:47:08
问题 I have this sort of function: someFunction.someMethod('param1', function(err, res1, res2) { req.method(res1, function(err) { if (!err) { console.log('Yes!'); } }); })(req, res); // <-- This one's the problem! Now when I try to promisify it: var a = Promise.promisify(someFunction.someMethod); a('param1').spread(function(res1, res2) { console.log('Yes!'); }).catch(function(err) { }); it doesn't work anymore, because I cannot put the (req, res) at the end of it. How to achieve this? 回答1: I would

Wait for promises from a for loop

旧城冷巷雨未停 提交于 2020-01-06 19:34:57
问题 The following code does not really do what I want. function doIt () { return new Promise (function (resolve, reject) { var promises = []; db.transaction(function(tx1){ tx1.executeSql(function(tx2, rs) { for (var i = i; i < N; i++) { promises.push(db.transaction(function(tx3){ ... })); } }); }); Promise.all(promises).then(resolve); }); } Now it does not work, because Promise.all() gets executed, before all promises are in the array, at least I think that's correct. Is there a elegant way to

Best way to handle early returns in chains of promises?

别说谁变了你拦得住时间么 提交于 2020-01-05 17:59:27
问题 What's the best way to handle early returns in Bluebird without throwing an error. For example, I have a conditional in the following: things.find(1) .then(function(thing) { if (thing.condition === true) { return thing } else { // early return? } }) .then(function(thing) { return doStuff(thing) }) 回答1: Once a .then chain is formed, its natural behaviour is to run progressively to completion as each of its stages settles. For an "early return" (not a good phrase but we know what you mean), you

Best way to handle early returns in chains of promises?

送分小仙女□ 提交于 2020-01-05 17:58:08
问题 What's the best way to handle early returns in Bluebird without throwing an error. For example, I have a conditional in the following: things.find(1) .then(function(thing) { if (thing.condition === true) { return thing } else { // early return? } }) .then(function(thing) { return doStuff(thing) }) 回答1: Once a .then chain is formed, its natural behaviour is to run progressively to completion as each of its stages settles. For an "early return" (not a good phrase but we know what you mean), you

Facing difficulty to stop the stream in child process spawn in Node.js?

橙三吉。 提交于 2020-01-05 09:14:13
问题 I am trying to use Node.js Child process spawn. Below code will execute the certain shell commands and read the data as buffer streams listener provided by spawn process. Bluebird node promise module is used to wrap around the child process. var execSpawn = require('child_process').spawn; var Promise = require('bluebird'); spawnAction = function(path, cmd, cb){ return function(resolve, reject){ cmdExec = execSpawn(path, cmd); var fileData = {} var count = 0; cmdExec.stdout.on('data', function

Facing difficulty to stop the stream in child process spawn in Node.js?

对着背影说爱祢 提交于 2020-01-05 09:13:30
问题 I am trying to use Node.js Child process spawn. Below code will execute the certain shell commands and read the data as buffer streams listener provided by spawn process. Bluebird node promise module is used to wrap around the child process. var execSpawn = require('child_process').spawn; var Promise = require('bluebird'); spawnAction = function(path, cmd, cb){ return function(resolve, reject){ cmdExec = execSpawn(path, cmd); var fileData = {} var count = 0; cmdExec.stdout.on('data', function

Use bluebird for mongoose, got “.bind is not a function”

十年热恋 提交于 2020-01-05 08:03:43
问题 I use bluebird for mongoose: const Promise = require("bluebird"); const mongoose = require("mongoose"); mongoose.Promise = Promise; And I want to use Promise.bind to share variable in the promise chain: function getAutherOfBook(name) { return Book.findOne( { name: name }, "-_id auther") .then(doc => { return doc.auther; }); }; function geNationalityOfAuther(name) { return Auther.findOne( { name: name }, "-_id nationality") .then(doc => { return doc.nationality; }); }; getAutherOfBook("The