Questions tagged [q]

The Q.js JavaScript promise library. Q catches errors and uses a `then` technique to handle JavaScript callbacks and exceptions. Do *not* use for Angular's $q, use [angular-promise] instead! Also do *not* use for kx System's kdb+/q, use [kdb] or [q-lang] instead! Lastly, this tag should not be used for questions regarding Android 10 - use the [android-10.0] tag instead!

A JavaScript library for the browser and Node.js for making and composing asynchronous promises. Q catches errors and uses a then technique to handle JavaScript callbacks and exceptions.

1381 questions
647
votes
3 answers

What is the explicit promise construction antipattern and how do I avoid it?

I was writing code that does something that looks like: function getStuffDone(param) { | function getStuffDone(param) { var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) { // or = new…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
507
votes
11 answers

Aren't promises just callbacks?

I've been developing JavaScript for a few years and I don't understand the fuss about promises at all. It seems like all I do is change: api(function(result){ api2(function(result2){ api3(function(result3){ // do work …
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
498
votes
19 answers

How to check if an object is a Promise?

Whether it's an ES6 Promise or a Bluebird Promise, Q Promise, etc. How do I test to see if a given object is a Promise?
theram
  • 5,503
  • 2
  • 13
  • 14
414
votes
36 answers

Resolve promises one after another (i.e. in sequence)?

Consider the following code that reads an array of files in a serial/sequential manner. readFiles returns a promise, which is resolved only once all files have been read in sequence. var readFile = function(file) { ... // Returns a…
XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151
392
votes
6 answers

What's the difference between returning value or Promise.resolve from then()

What is the difference between: new Promise(function(res, rej) { res("aaa"); }) .then(function(result) { return "bbb"; // directly returning string }) .then(function(result) { console.log(result); }); and this: new…
spirytus
  • 10,726
  • 14
  • 61
  • 75
253
votes
1 answer

Are there still reasons to use promise libraries like Q or BlueBird now that we have ES6 promises?

After Node.js added native support for promises, are there still reasons to use libraries like Q or BlueBird? For example if you are starting a new project and let's assume in this project you don't have any dependencies that use these libraries,…
Murat Ozgul
  • 11,193
  • 6
  • 29
  • 32
129
votes
4 answers

Why is 'this' undefined inside class method when using promises?

I have a javascript class, and each method returns a Q promise. I want to know why this is undefined in method2 and method3. Is there a more correct way to write this code? function MyClass(opts){ this.options = opts; return this.method1() …
SteamDev
  • 4,294
  • 5
  • 20
  • 29
116
votes
9 answers

How do you properly return multiple values from a Promise?

I've recently run into a certain situation a couple of times, which I didn't know how to solve properly. Assume the following code: somethingAsync() .then( afterSomething ) .then( afterSomethingElse ) function afterSomething( amazingData ) { …
Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
106
votes
3 answers

Angularjs $q.all

I have implemented the $q.all in angularjs, but I can not make the code work. Here is my code : UploadService.uploadQuestion = function(questions){ var promises = []; for(var i = 0 ; i < questions.length ; i++){ var…
themyth92
  • 1,743
  • 2
  • 17
  • 23
104
votes
2 answers

How does Angular $q.when work?

Can some one explain me how does $q.when work in AngularJS? I'm trying to analyse how $http work and found this: var promise = $q.when(config); And here is config object from Chrome console: Object {transformRequest: Array[1], transformResponse:…
SET001
  • 11,480
  • 6
  • 52
  • 76
91
votes
2 answers

return value after a promise

I have a javascript function where I want to return the value that I get after the return method. Easier to see than explain function getValue(file){ var val; lookupValue(file).then(function(res){ val = res.val; } return…
pedalpete
  • 21,076
  • 45
  • 128
  • 239
87
votes
5 answers

How to Check Whether an Angular $q promise Is Resolved

I understand that typically one would just attach continuation code with a then() call and chain behaviour when using promises. However, I want to kick off a promise-wrapped asynchronous call and then separately kick off a 3-second $timeout() so I…
blaster
  • 8,876
  • 11
  • 48
  • 77
85
votes
2 answers

Rendering React components with promises inside the render method

I have a component which gets a collection of items as props and maps them to a collection of components which are rendered as children of a parent component. We use images stored in WebSQL as byte arrays. Within the map function I get an image Id…
Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
82
votes
14 answers

While loop with promises

What would be the idiomatic way to do something like a while loop with promises. So: do something if the condition still stands do it again repeat then do something else. dosomething.then(possilblydomoresomethings).then(finish) I've done it this…
Grummle
  • 1,364
  • 1
  • 12
  • 21
47
votes
1 answer

Problems inherent to jQuery $.Deferred (jQuery 1.x/2.x)

@Domenic has a very thorough article on the failings of jQuery deferred objects: You're missing the Point of Promises. In it Domenic highlights a few failings of jQuery promises in comparison to others including Q, when.js, RSVP.js and ES6…
Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
1
2 3
92 93