bluebird

wrap node-multiparty with promisifyAll bluebird

我只是一个虾纸丫 提交于 2019-12-11 03:48:40
问题 I'm new with node.js, I'm trying to wrap node-multiparty callbacks with bluebird but the parse function signature of multiparty is function(err, fields, files) and the promise signature requires just one return value. I'm sure there is a way to do this but I haven't found anything yet. Thanks in advance! 回答1: Use spread instead of then . Taking from the example in their README: var Promise = require('bluebird'); var multiparty = Promise.promisifyAll(require('multiparty')); var http = require(

Bluebird: getting the results of each()

末鹿安然 提交于 2019-12-11 03:35:08
问题 Here's something I don't understand about .each() . The function returns a promise that yields the original array instead of an array of results from the callback. How you get the results then? .map() isn't what I'm looking for since the elements need to be processed in order. var input = [1, 2, 3]; return promise.each(input, causeSideEffect).then(function() { /* return true if any side effect occurred */ }); In the example, causeSideEffect() returns a promise of a boolean, indicating whether

Returning values from Promise(Bluebird) in Node.js

∥☆過路亽.° 提交于 2019-12-11 03:08:51
问题 I am learning Node.js and was looking at the Promise module. I am very new at this so please bare with bad code conventions and stuff. I am trying to build a REST Client API using restify. I have attached my Client Code below: Here is my Client: // local functions function loadResult(err, req, res, obj) { //console.log(err, req, res, obj); var result = { 'err': err, 'text': obj }; console.log(result); return result; } function getClient() { if (client) { return client; } else { client =

How to turn nested callback into promise?

浪子不回头ぞ 提交于 2019-12-11 01:38:12
问题 Recently I started using pg-promise with bluebird library. I have always been nesting callback and handling err in each callback. I find that the catch statement in promise looks really neat. I am not sure if it possible to turn this code to promise base? username = username.toUpperCase(); let text = "SELECT * FROM users WHERE username = $1"; let values = [username]; database.one(text, values).then(function (userObject) { // ANY WAY TO TURN this nested bycrypt into promise chain? bcrypt

How to use bluebird map and return object

纵饮孤独 提交于 2019-12-11 01:33:52
问题 I need to use the map bluebird (Promise lib) but to return an object, instead of an array. Currently, I'm using it like this (but it is wrong): return new Promise(function(resolve, reject) { return Promise.map(files, function(file) { // LOGIC resolve({ // object here }) }, { concurrency: 3000 }) }); The reason is simple: I need the concurrency: 3000, and I need to return the object. So the new Promise is to return the object, and not an array, and the map is because of the concurrency. Are

Bluebird promise resolve(data) is undefined in client code

老子叫甜甜 提交于 2019-12-10 19:22:25
问题 Hiyas. I have a simple app whereby a client is expecting a promise as a result, but upon calling the resolve() method, the promise keeps returning undefined as the result. The client code: UsersRepo.findOneAsync({id: id}).then(function(err, result) { console.log("UserService promise resolution", err, result); }); This outputs "null" and "undefined", for err and result, respectively The code that is doing the work and returning the promise: findOneAsync: function(args) { var where = ""; /

Use Promise.all() in AWS lambda

岁酱吖の 提交于 2019-12-10 17:49:12
问题 I don't understand why my promise.all() is not executed in Lambda while in development already work. On AWS Lambda, it's already timeout because Promise.all() are not completed and I don't receive the new messages in my SQS queue. However, the console.log(promises) returned : 2017-01-29T22:55:46.191Z 0e82eeaf-e676-11e6-b69d-73a6bbd86272 [ Promise { <pending> } ] var url = require('url'); var AWS = require('aws-sdk'); // var Promise = require("bluebird"); exports.handler = function (event,

Run Bluebird Promises Sequentially, without return values?

和自甴很熟 提交于 2019-12-10 17:41:16
问题 This question has been asked in a variety of ways, but not quite as simply. How would this Promise.all be rewritten so that promise1 runs completely before promise2 ? var promise1 = function() { .. lots of promise stuff }; var promise2 = function() { .. lots more promise stuff }; Promise.all([promise1, promise2]).then(function() { log.info("ran promise1 & promise2"); }); Promise.all runs promise1 & promise2 in parallel. 回答1: You can use Promise.map with concurrency option set to 1. var

wait for all promises to finish in nodejs with bluebird

你说的曾经没有我的故事 提交于 2019-12-10 17:21:25
问题 What's the best way to wait for all promises to finish in nodejs with bluebird? Lets say I want to select records from database and store them in redis. I came up with this loadActiveChannels: function() { return Knex('game_channels as ch') .where('ch.channel_state', '>', 0) .then(function(channels) { var promises = []; for(var i=0; i<channels.length; i++) { var promise = redis.hmsetAsync("channel:"+channels[i].channel_id, _.omit(channels[i], 'channel_id')) promises.push[promise]; } return

chain array of promises with bluebird

三世轮回 提交于 2019-12-10 14:29:49
问题 I'm a working my way with promises and I'm stuck with my use case. I have an array of transformer functions (each function is a promise and modifies some JSON structure). Let me show some code. Lets say this is my JSON structure (array) var data = [{a: 1, b:2}, {a:3, b:4}]; transformFunction is definition of transform functions modifying the data a certain way. The two functions adds c and d property to the above JSON structure: var transformFunctions = { // transform1: function (data) { //