bluebird

Swap order of arguments to “then” with Bluebird / NodeJS Promises

房东的猫 提交于 2019-12-12 03:06:22
问题 I have a function which asynchronously grabs a value from a server: var request = require('request'); Promise.promisifyAll(request); function getValue(){ return request.getAsync('http://www.google.com') .then(function(resp){ return resp.body; }) .catch(function(err){ thow err; }); } I want to take this value and dump it to a file: var fs = require('fs'); Promise.promisifyAll(fs); getValue().then(fs.writeFileAsync, "file.html"); The problem is that fs.writeFileAsync expects parameter one to be

Bluebird Promise Retry DocumentDB request

不羁岁月 提交于 2019-12-12 02:54:35
问题 I'm trying to rewrite a retry function with callbacks into a Bluebird promise one but can't seem to get my head around the correct way of doing this. At the bottom is the working callback function for retrying Azure DocumentDB when limit is met. I'm trying to use promises in the function itself but it returns before reaching the "Then". Any hints on how to tackle this or if performance is affected by using catch this way would be appreciated. Thank you! "readDocRetry": function(id, retries) {

How do I access previous promise results in a .then() chain?

浪子不回头ぞ 提交于 2019-12-12 02:49:14
问题 I have restructured my code to promises, and built a wonderful long flat promise chain , consisting of multiple .then() callbacks. In the end I want to return some composite value, and need to access multiple intermediate promise results . However the resolution values from the middle of the sequence are not in scope in the last callback, how do I access them? function getExample() { return promiseA(…).then(function(resultA) { // Some processing return promiseB(…); }).then(function(resultB) {

Wrapping imported construtor in a promise

人盡茶涼 提交于 2019-12-12 02:17:56
问题 I have the same problem as Promisify imported class (constructor) with bluebird in ES6 + babel. The accepted answer seems to have a bug but I couldnt comment on that thread (not enough reputation points), so am creating a new question. The bug seems to be in: var o = new lib.C(); // get object o.then(function(data){ // access data }); I get a run-time error: ~/node_modules/bluebird/js/main/promise.js:114 return this._then(didFulfill, didReject, didProgress, ^ TypeError: undefined is not a

How to reuse a mongo connection with promises

不想你离开。 提交于 2019-12-11 11:49:40
问题 How can I change things around in my db connection call so that I can do db.collection() : // Create a Mongo connection Job.prototype.getDb = function() { if (!this.db) this.db = Mongo.connectAsync(this.options.connection); return this.db; }; // I want to be able to do this Job.prototype.test = function() { return this.db.collection('abc').findAsync()... }; // Instead of this Job.prototype.test = function() { return this.getDb().then(function(db) { return db.collection('abc').findAsync()... }

Nested Promise execution is out of sync

半腔热情 提交于 2019-12-11 09:17:10
问题 I have a simple setup, illustrated in this fiddle: var doDelay = function(who) { return Promise.delay(50) .tap(function() { console.log(who + ' done'); }); }; Promise.resolve() .then(doDelay('a')) .tap(function() { console.log('a done2'); }) .then(doDelay('b')) .tap(function() { console.log('b done2'); }) .then(function() { console.log('all done!'); }); The output is: a done2 b done2 all done! a done b done But I expect: a done b done a done2 b done2 all done! What am I doing wrong? 回答1: As

How do you call a function with parameters in a .then function in a JavaScript promise string?

喜你入骨 提交于 2019-12-11 05:51:58
问题 I am converting my AWS lambda functions, written in node.js, to use promises instead of callbacks. I'm wrapping all of my functions in the handler with the handler code. I'm trying to break out simple functions so I can have as flat a promise chain as possible in the handler code. I'm stuck at one point where I have a .then() that returns a value that I need to pass to one of my functions, which has been promisified, along with other parameters. I have searched high & low but can't find an

Cannot read property of undefined after Promise.promisify

醉酒当歌 提交于 2019-12-11 05:11:27
问题 let nasPath = ""; return getFamInfo(args.familyID) .then(function (famInfo) { nasPath = //some code involving famInfo here return getSFTPConnection(config.nasSettings); }).then(function (sftp) { const fastPutProm = Promise.promisify(sftp.fastPut); return fastPutProm(config.jpgDirectory, nasPath, {}); }); If I put a breakpoint after const fastPutProm = Promise.promisify(sftp.fastPut); , fastPutProm is a function with three arguments. But when I try to run this code, I get a TypeError: Cannot

Retrieve data from a callback thats in a promise?

两盒软妹~` 提交于 2019-12-11 04:29:54
问题 I have the following piece of code right now: const Promise = require('bluebird'); const readFile = Promise.promisify(fs.readFile); recordPerfMetrics: function(url) { var self = this; var perf, loadTime, domInteractive, firstPaint; var perfData = {}; readFile('urls.txt', 'UTF-8').then(function (urls, err) { if (err) { return console.log(err); } var urls = urls.split("\n"); urls.shift(); urls.forEach(function(url) { console.log(url); self.getStats(url).then(function(data) { data = data[0];

Bluebird Promises with Event Emitter

你离开我真会死。 提交于 2019-12-11 04:18:20
问题 I am fairly new with using Bluebird promises. I was trying to use them over an emitter. However, I am stuck on how to handle errors. I have a stream object which is the emitter. Code is as below - return new Promise((resolve, reject) => { var onDocFunc = doc => { //JSON.parse('*'); // some logic goes in here to construct setStmt bulk.find(query).upsert().update({$set: setStmt}); count++; if (count % bulkSize == 0) { stream.pause(); var execute = Promise.promisify(bulk.execute); execute()