Questions tagged [promise]

Promises are a tactic for deferred computing, suitable for several styles of concurrency: thread and event loop concurrency for local computation, and both synchronous and asynchronous remote messaging. A promise represents the eventual result of an asynchronous operation. The primary way of working with promises is through a method which registers transformations from the promise's eventual value or failure reason to a new promise.

In computer science, future, promise, and delay refer to constructs used for synchronizing in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown, usually because the computation of its value is yet incomplete.

Promises are a common construct in with new built in language support. There are several popular implementations of the concept such as and . However, promises are not unique in JavaScript and similar patterns exist in many languages. Popular implementations exist in , , , , , , and most other languages. Some languages provide native language support for the construct.

Recurring questions:

Reading material:

Popular Implementations:

22514 questions
6
votes
2 answers

Accessing a previously fulfilled promise result in a promises chain

What is the correct pattern, when coding with promises, to access data coming from long before in a chain of promises? For example: do_A.then(do_B).then(do_C).then(do_D).then(do_E_WithTheDataComingFrom_A_And_C_OnlyWhen_D_IsSuccesfullyCompleted) My…
lOlive
  • 209
  • 1
  • 5
6
votes
2 answers

How to convert callback sample to deferred object?

I have a function that accepts a callback function where I pass the data back in. Can this converted to a deferred object for better practice? Here is what I got: var chapters; var getChapters = function (fnLoad) { //CACHE DATA IF…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
6
votes
1 answer

Angularjs $http wait for response

I am a newbie to javascript/angularjs. I want to show a bootstrap popover when mouseover is done on some elements. I created a directive for that (function(angular, app) { app.directive('popOver',["$window","$http",function($window,$http){ …
Anirudhan J
  • 2,072
  • 6
  • 27
  • 45
6
votes
1 answer

Using Promises in AngularJS Views

Mark Dalgleish wrote a nice little article about how to use promises in AngularJS views. Some people asked questions about this in the comments, but Mark didn't answer them (yet). Because I'm asking me the same question, I will ask on StackOverflow…
Pipo
  • 5,623
  • 7
  • 36
  • 46
6
votes
1 answer

angular.js deferred doesnt work in callback

Consider the following example: .service('movieGetter', ['$q', '$timeout', function ($q, $timeout) { this.getData = function () { var deferred = $q.defer(); $timeout(function(){ mock.getData(function(data){ …
Dimkin
  • 670
  • 2
  • 9
  • 22
6
votes
1 answer

Handling branches with Promises

I have a problem with jQuery 1.9.1 promises, where I potentially need conditional logic that will return another deferred and I'm not sure how to handle it. This was my best attempt, but as the comments indicate below, when I hit the else branch, I…
rooftop
  • 3,031
  • 1
  • 22
  • 33
6
votes
2 answers

jQuery.Deferred() / Promises functionality in GWT?

Recently, I've discovered I really like jQuery.Deferred() and the features it gives you to handle asynchronous flow control (Promises). I suppose the things I like the most ar the callback hooks for an Ajax request (.done() and .fail() ) and…
blong
  • 2,815
  • 8
  • 44
  • 110
6
votes
4 answers

How do I dynamically add a deferred to the promise from this jsFiddle?

Regarding this jsFiddle, I am trying to dynamically add a "deferred" which is created when an event triggers, so the done callback is only called when all deferred are resolved, including those added later: Relevant code: var promises = […
Hontoni
  • 1,332
  • 1
  • 16
  • 27
6
votes
1 answer

Node.js Q promises, why use defer() when you can use this()?

I wanted to do something like: somePromiseFunc(value1) .then(function(value2, callback) { // insert the next then() into this function: funcWithCallback(callback); }) .then(function(dronesYouAreLookingFor){ // Have a…
Redsandro
  • 11,060
  • 13
  • 76
  • 106
6
votes
1 answer

mongoose and q promises

I'm working from the mongoose/q promises framework sample here, but seem to have some issues with the nfbind when trying to use findOne, mainly since the samples from the Q framework don't seem to match those in the gist. My code: var mongoose =…
mlaccetti
  • 1,726
  • 2
  • 18
  • 17
6
votes
2 answers

jQuery ajax and the "when" function

I have some (lets say 5) ajax requests that I run at the same time by using jQuery.ajax function. Now I would like to synchronize and aggregate their results and I used jQuery.when function to achieve this. My problem is that $.when returns as soon…
Riana
  • 689
  • 6
  • 22
6
votes
1 answer

Breaking out of a Q promise in Node.js when performing a callback?

Please excuse my newbishness with the concept of promises. I'm using the Q module in Node.js. I have a function that is meant to call a callback once it has performed all the necessary steps. The problem occurs when I want to call the callback…
Zane Claes
  • 14,732
  • 15
  • 74
  • 131
6
votes
2 answers

jQuery promise in function with each()

I'm tryin to call a own function and wait until its finishes. After the transitions end I want to start the next function. please have a look in my jsfiddle http://jsfiddle.net/hrm6w/ The console.log("upper finished") should start after…
felixaburg
  • 215
  • 1
  • 3
  • 8
5
votes
1 answer

Why does js_sys Promise::new require FnMut?

js_sys exposes JavaScript Promise via a function pub fn new(cb: &mut dyn FnMut(Function, Function)) -> Promise;. Per my reading of the MDN documentation, there's nothing suggesting that the executor function will be called more than once, yet js_sys…
Huckle
  • 1,810
  • 3
  • 26
  • 40
5
votes
3 answers

Why Node.js does not wait for promise to resolve before exiting?

When I execute the following code, I thought Node.js will wait till we call the resolve. Till then the myPromise will be in the state. Then how come node exits before it resolves? The following code exits immediately! const myPromise = new…
Wajahath
  • 2,827
  • 2
  • 28
  • 37