bluebird

How to cast jQuery $.ajax calls to Bluebird promises without the deferred anit-pattern

十年热恋 提交于 2019-12-21 03:17:09
问题 Right now I use promise.deferred in a core file. This allows me to resolve promises at a central location. I've been reading that I may be using an anti-pattern and I want to understand why it is bad. so in my core.js file I have functions like this: var getMyLocation = function(location) { var promiseResolver = Promise.defer(); $.get('some/rest/api/' + location) .then(function(reponse) { promiseResolver.resolve(response); )} .catch(function(error) { promiseResolver.reject(error); }); return

Bluebird Promise Scope

爷,独闯天下 提交于 2019-12-20 05:32:12
问题 I have just started using promises in attempt to cleanup some 'callback hell'. I've decided on trying bluebird and I am running it in the browser but immediately ran into scoping problems. Is there a way of setting the thisArg in a new Promise? The below example shows that the 'this' value inside the promise resolver is set to the browser window, but I'd like it set to the surrounding scope so I can easily access member variables. I noticed there is a .bind() method but it only scopes the

How to pass a third argument to a callback using Bluebird.js nodeify

允我心安 提交于 2019-12-19 09:51:12
问题 With a little help I've arrived at the following code to promisify a passport.js login strategy. var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; var Promise = require('bluebird'); var bcrypt = require('bcrypt'); var db = require('./db').db; //users are stored in mongo //I'm using bluebird.js for promises var users = Promise.promisifyAll(db.users); var compare = Promise.promisify(bcrypt.compare); // This strategy is used by passport to handle logins

How to do parallel async multiple requests at once with Promises in Node

不想你离开。 提交于 2019-12-19 09:24:04
问题 Array and loops through but I want to be able to run all of them in parallel instead as I don't want to run one after another. I basically want to store all endpoint calls status codes, body and time as array and return them as results regardless of there are errors or not in the endpoint. I'm using Bluebird, how can I use its features to solve this issue? 回答1: You can use Promise.map with .bind : function getComponentStatuses(componentsToCheck) { return Promise.map(componentsToCheck,

Bluebird promisify and callback with no error argument

别来无恙 提交于 2019-12-19 00:56:23
问题 I'm trying to promisify a 3rd party library that doesn't use the callback(err, data) pattern. Instead they always return callback(data) and throw on errors. Promise.promisifyAll(horse); var p = Promise.defer(); horse.drinkAsync() .error(function(data) { p.fulfill(data); }) .catch(function (err) { console.error('error occured', err); }); return p.promise; What is a nice way to wrap such a behavior with promises and still have it look ok and allow to catch the thrown error? The catch clause

Download an image using node-request, and fs Promisified, with no pipe in Node.js

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 22:35:21
问题 I have been struggling to succeed in downloading an image without piping it to fs. Here's what I have accomplished: var Promise = require('bluebird'), fs = Promise.promisifyAll(require('fs')), requestAsync = Promise.promisify(require('request')); function downloadImage(uri, filename){ return requestAsync(uri) .spread(function (response, body) { if (response.statusCode != 200) return Promise.resolve(); return fs.writeFileAsync(filename, body); }) .then(function () { ... }) // ... } A valid

Can I use other promise implementations in the Parse JavaScript SDK?

谁说胖子不能爱 提交于 2019-12-18 14:56:02
问题 I am concerned about references I have seen to Parse using JQuery-compatible promises, as I have read that jQuery promises allow consumers to mutate the state of the promise. Is it possible to use another promise implementation that is known to be Promises/A+ compliant (e.g. the ECMAScript 6 implementation, or Bluebird) with the Parse JavaScript SDK? Normally I would assume that this isn’t possible, but in v1.4.2 of the Parse JavaScript SDK, the implementation of Parse.Promise defines the

Bluebird, promises and then()

时间秒杀一切 提交于 2019-12-18 10:14:29
问题 I've been only using bluebird for a few days but I want to go over all my old code and promisify it :) My problem is that I still don't fully grasp the flow of then() commands. Consider these two blocks: A methodThatReturnsAPromise().then(task2).then(task3); B var promise = methodThatReturnsAPromise(); promise.then(task2) promise.then(task3); in scenario A task3 will get the result of task2 ? In B they all get the result of the first promise? How does the second one differ from running

How do Promises/A+ implementations vary?

不打扰是莪最后的温柔 提交于 2019-12-18 07:41:07
问题 What aspects of a promise library does the spec not cover? What kind of things vary between implementations? Please illustrate with examples of actual differences (eg between Bluebird and Q). 回答1: Almost everything. The Promises/A+ spec is aimed for promise interoperability, it's built so promise libraries (and now, native promises) can talk to each other . The idea is for it to be possible to predict how a promise behaves and to define how promises are assimilated by other libraries. Quoting

Way to find if function will return promise

元气小坏坏 提交于 2019-12-18 07:07:30
问题 Below I have a function that returns a promise that resolves true . Is there any way I can find out if a function will return a promise? var myPromiseFunction = function(){ return Promise.resolve(true) } myPromiseFunction().then(function(value){ console.log(value) // => true }) function mySyncFunction(){ return "hello" } willReturnPromise(myPromiseFunction) // => true willReturnPromise(mySyncFunction) // => false 回答1: Is there any way I can find out if a function will return a promise? No