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
20
votes
2 answers

Promises - error callback vs. catch

Can somebody tell me if there is a difference between using an error callback vs. a catch function, when using $q.promise please? E.g. are the two snippets of code functionally equivalent? function doSomething0() { var deferred = $q.defer(); …
keldar
  • 6,152
  • 10
  • 52
  • 82
18
votes
1 answer

Issue in returning data retrieved from DB queries called in the loop

I making multiple mongoDB queries in loop. and want to send the all results as one data array.But when I simple use the return for send the data it simply return undefined and do not wait for results of all DB request. I also tried to use q.moulde…
17
votes
7 answers

How do I sequentially chain promises with angularjs $q?

In the promise library Q, you can do the following to sequentially chain promises: var items = ['one', 'two', 'three']; var chain = Q(); items.forEach(function (el) { chain = chain.then(foo(el)); }); return chain; however, the following doesn't…
redgeoff
  • 3,163
  • 1
  • 25
  • 39
17
votes
5 answers

AngularJS - fail resilence on $q.all()

I'm trying to fill some local data resolving a series of remote calls. When every promise is resolved, I load the data and proceed. The method $q.all( [] ) does exactly this: $q.all([ this.getUserInfo(11) …
domokun
  • 3,013
  • 3
  • 29
  • 55
17
votes
1 answer

Is there a pure Promise-based approach for mapping/concatenating collections?

async vs. Q generally I'm learning Node.js development, and trying to wrap my brain around strategies for managing asynchronous "callback hell". The two main strategies I've explored are Caolan McMahon's async module, and Kris Kowal's promise-based…
Steve Perkins
  • 11,520
  • 19
  • 63
  • 95
17
votes
1 answer

Create an empty promise

I have a function in a chain of promises that may or may not do something. E.g. getYear().then(function(results){ if(results.is1999) return party(); else return Q.fcall(function(){/*do nothing here*/}); }).then(sleep) Where getYear,…
prauchfuss
  • 1,930
  • 3
  • 17
  • 20
17
votes
5 answers

How to use "q" module for refactoring mongoose code?

I'm using mongoose to insert some data into mongodb. The code looks like: var mongoose = require('mongoose'); mongoose.connect('mongo://localhost/test'); var conn = mongoose.connection; // insert…
Freewind
  • 193,756
  • 157
  • 432
  • 708
16
votes
1 answer

Recursive Promises?

I would like to iterate over all files located in the HTML 5 file system and have some event get started once the iteration is complete. Since this is async + promises im having a hard time trying to grasp how it should work. I am using a angularJS…
Ivan Bacher
  • 5,855
  • 9
  • 36
  • 56
16
votes
3 answers

How to correctly chain conditional(?) promises with Q.js

I've still not quite got a complete understanding of promises so apologies if this is a simple misunderstanding. I have a function for deleting an item on a page but I have a specific behaviour depending on the state of the page. Psuedo code-wise…
Adam
  • 1,011
  • 1
  • 10
  • 25
16
votes
2 answers

AngularJS Promises, $q, defer

EDIT The first answer is the elegant one, but, as stated a few times in this question and another questions on stackoverflow, the problem is that the service and the controller run their thing before the data actually arrives. (Last comment on the…
Arthur Kovacs
  • 1,710
  • 2
  • 17
  • 24
16
votes
3 answers

javascript promise not passing all arguments (using Q)

I am having trouble passing all arguments. My promise callback only receives one instead of three: var asyncFunction= function(resolve) { setTimeout(function() { resolve("Some string that is passed", "and another", "third"); },…
Nick
  • 11,483
  • 8
  • 41
  • 44
15
votes
1 answer

Q 2.0.0 installed with Bower causes Uncaught ReferenceError: require is not defined

I am using Q in a project, and I am using bower to manage my JS dependencies. I am including Q 2.0.0 with bower in bower.json "dependencies" : { "q": "~2.0.0" } In my index.html, I include Q with a script tag
binarygiant
  • 6,362
  • 10
  • 50
  • 73
15
votes
2 answers

q.js: difference between resolve() and fulfill()

I'm still unclear on the difference between calling a resolver's resolve() vs fulfill()? I see both the functions and the terms "resolve a promise" and "fulfill a promise" batted around a lot. When should I be using each?
alecf
  • 603
  • 1
  • 4
  • 10
15
votes
3 answers

Can I make a synchronous promise in the JavaScript Q library?

I want to do something like the following: delay( 2500 ) .then( function () { console.log( "Step 1 done" ) } ) .then( delay( 7500 ) ) .then( function () { console.log( "Step 2 done" ) } ); So implementation of delay has been demonstrated many…
PP.
  • 10,764
  • 7
  • 45
  • 59
14
votes
2 answers

JavaScript Promise/Defer in Chrome

I use the Q library that supports the Promise specification well. But I also try to use the Promise class that has been implemented in Chrome not long ago (experimentally). There's the defer function in Q that can be used for creating a…
shadeglare
  • 7,006
  • 7
  • 47
  • 59
1 2
3
92 93