bluebird

How to use promise bluebird in nested for loop?

一个人想着一个人 提交于 2020-01-26 02:04:09
问题 I need to use bluebird in my code and I have no idea how to use it. My code contains nested loops. When the user logs in, my code will run. It will begin to look for any files under the user, and if there are files then, it will loop through to get the name of the files, since the name is stored in a dictionary. Once it got the name, it will store the name in an array. Once all the names are stored, it will be passed along in res.render(). Here is my code: router.post('/login', function(req,

Convert promise to synchronous function

三世轮回 提交于 2020-01-24 20:52:26
问题 If I have a simple function like this one below addTwo I can use bluebird's Promise.method(addTwo) to make it a promise, even though it doesn't perform any async operations. Is there any way to do the opposite of this? function addTwo(num){ return num + 2 } var newValue = addTwo(2) // => 4 addTwoPromise = Promise.method(addTwo) addTwoPromise(2).then(function(newValue){ console.log(newValue) // == 4 }) Is there any way to convert addTwoPromise from a promise to a synchronous function again? I

How to wait for a bluebird promise to settle in multiple locations?

亡梦爱人 提交于 2020-01-24 19:00:06
问题 I have a situation where a bunch of functions are needing to wait for a promise to settle because it's the init function; self.init=new Promise(function(resolve){ //do stuff, take awhile resolve(); }); But, while it's init'ing, the async nature means other functions that depend on it being init are being called. I want those functions to wait for the init to finish, then continue. I tried doing this inside each function function doSomethingUseful(){ self.init.reflect().then(function () { //do

Cancel a delayed Bluebird promise

北慕城南 提交于 2020-01-24 18:03:29
问题 How to reject a delayed Promise: const removeDelay = Promise.delay(5000).then(() => { removeSomething(); }); //Undo event - if it is invoked before 5000 ms, then undo deleting removeDelay.reject(); // reject is not a method 回答1: Bluebird v3 We no longer need to declare a Promise as 'cancellable' (documentation): no setup code required to make cancellation work Simply call cancel on a Promise: const promise = new Promise(function (_, _, onCancel) { onCancel(function () { console.log("Promise

Cancel a delayed Bluebird promise

自古美人都是妖i 提交于 2020-01-24 18:03:01
问题 How to reject a delayed Promise: const removeDelay = Promise.delay(5000).then(() => { removeSomething(); }); //Undo event - if it is invoked before 5000 ms, then undo deleting removeDelay.reject(); // reject is not a method 回答1: Bluebird v3 We no longer need to declare a Promise as 'cancellable' (documentation): no setup code required to make cancellation work Simply call cancel on a Promise: const promise = new Promise(function (_, _, onCancel) { onCancel(function () { console.log("Promise

Promise.each without bluebird

三世轮回 提交于 2020-01-24 02:14:42
问题 I need using Promise.each on bluebird . But when I see the bundle files, I'm actually thinking twice using bluebird or not. Can anyone give me an example using function like bluebird Promise.each without dependencies. 回答1: Sure: Promise.each = function(arr, fn) { // take an array and a function // invalid input if(!Array.isArray(arr)) return Promise.reject(new Error("Non array passed to each")); // empty case if(arr.length === 0) return Promise.resolve(); return arr.reduce(function(prev, cur)

Promisify imported class (constructor) with bluebird in ES6 + babel

安稳与你 提交于 2020-01-23 06:30:28
问题 Suppose I created or have a node.js library lib.js export class C { constructor(value, callback) { callback(false, `Hello ${value}`); } task(value, callback) { callback(false, "returned " + value); } } The important part is that the classes' constructor needs to accept a callback as it does database connections and file I/O. If I now import and use the library callback-style, everything is fine (see c1 below). I would really like to promisify the library where I use it to make object

How to properly deal with promisifyAll in typescript?

限于喜欢 提交于 2020-01-21 06:47:29
问题 Consider the following code: import redis = require('redis'); //Has ambient declaration from DT import bluebird = require('bluebird'); //Has ambient declaration from DT bluebird.promisifyAll((<any>redis).RedisClient.prototype); bluebird.promisifyAll((<any>redis).Multi.prototype); const client = redis.createClient(); client.getAsync('foo').then(function(res) { console.log(res); }); getAsync will error out because it's created on the fly and not defined in any .d.ts file. So what is the proper

Promise returns undefined

风格不统一 提交于 2020-01-20 06:57:06
问题 I know you can't make an asynchronous function behave synchronously but how do I add some kind of order to my promises chain? One result relies on the previous promise value and when that doesn't happen I get an undefined error. It's an http request so it is relying on external factors like how fast my connection can execute the request, etc. module.exports.movieCheck = function(authToken) { return request({ method : 'GET', uri : 'https://graph.facebook.com/' + profileID + '/posts?fields

How do I convert an existing callback API to promises?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-17 18:01:44
问题 I want to work with promises but I have a callback API in a format like: 1. DOM load or other one time event: window.onload; // set to callback ... window.onload = function() { }; 2. Plain callback: function request(onChangeHandler) { ... } request(function() { // change happened ... }); 3. Node style callback ("nodeback"): function getStuff(dat, callback) { ... } getStuff("dataParam", function(err, data) { ... }) 4. A whole library with node style callbacks: API; API.one(function(err, data)