Questions tagged [deferred]

Deferred objects simplify callback management in an asynchronous environment, mitigating the "Pyramid of Doom" that results from too many levels of nested callbacks.

Deferred objects simplify callback management in an asynchronous environment, mitigating the "Pyramid of Doom" that results from too many levels of nested callbacks. A deferred object exposes a promise as interface but internally keeps a system for changing the status of the promise.

Example libraries:

jQuery: http://api.jquery.com/jQuery.Deferred/
Node.js / Vanilla JS: https://github.com/kriskowal/q

1131 questions
322
votes
5 answers

What are the differences between Deferred, Promise and Future in JavaScript?

What are the differences between Deferreds, Promises and Futures? Is there a generally approved theory behind all these three?
Tower
  • 98,741
  • 129
  • 357
  • 507
141
votes
4 answers

AngularJS : Where to use promises?

I saw some examples of Facebook Login services that were using promises to access FB Graph API. Example #1: this.api = function(item) { var deferred = $q.defer(); if (item) { facebook.FB.api('/' + item, function (result) { …
Mak
  • 19,913
  • 5
  • 26
  • 32
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
100
votes
4 answers

How to exit a go program honoring deferred calls?

I need to use defer to free allocations manually created using C library, but I also need to os.Exit with non 0 status at some point. The tricky part is that os.Exit skips any deferred instruction: package main import "fmt" import "os" func main()…
marcio
  • 10,002
  • 11
  • 54
  • 83
83
votes
4 answers

How to always run some code when a promise is fulfilled in Angular.js

In my Angular.js application, I'm running some asynchronous operation. Before it starts I cover the application with a modal div, then once the operation is complete, I need to remove the div, whether the operation was successful or not. Currently I…
laurent
  • 88,262
  • 77
  • 290
  • 428
83
votes
9 answers

How do I chain three asynchronous calls using jQuery promises?

I have three HTTP calls that need I need to make in a synchronous manner and how do I pass data from one call to the other? function first() { ajax() } function second() { ajax() } function third() { ajax() } function main() { …
John Mcdock
  • 1,219
  • 2
  • 11
  • 19
74
votes
4 answers

angular $q, How to chain multiple promises within and after a for-loop

I want to have a for-loop which calls async functions each iteration. After the for-loop I want to execute another code block, but not before all the previous calls in the for-loop have been resolved. My problem at the moment is, that either the…
SebastianRiemer
  • 1,495
  • 2
  • 20
  • 33
67
votes
2 answers

JavaScript naming convention for promises?

I feel it would be useful to have a naming convention for JavaScript variables which hold a promise. I don't generally like or advocate naming conventions beyond programming language standards, but in the style of programming where promises are…
jevakallio
  • 35,324
  • 3
  • 105
  • 112
58
votes
3 answers

Go: returning from defer

I want to return an error from a function if it panics (in Go): func getReport(filename string) (rep report, err error) { rep.data = make(map[string]float64) defer func() { if r := recover(); r != nil { …
jrichner
  • 699
  • 1
  • 5
  • 9
54
votes
3 answers

Simple approach to launching background task in Django

I have a Django website, and one page has a button (or link) that when clicked will launch a somewhat long running task. Obviously I want to launch this task as a background task and immediately return a result to the user. I want to implement this…
Marc
  • 3,386
  • 8
  • 44
  • 68
53
votes
2 answers

How to block for a javascript promise and return the resolved result?

I am obviously misunderstanding something about either the way js promises are resolved or about the semantics of "return." I am being called by a function that expects me to be synchronous - to return a value. Calculating that value requires some…
Chris Owens
  • 1,107
  • 1
  • 10
  • 15
48
votes
7 answers

How to chain execution of array of functions when every function returns deferred.promise?

I have created my first deferred object in Node.js using deferred module and it works great when I pass result to next function and trigger resolve and reject.How to chain execution of array of functions when every function returns deferred.promise…
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111
44
votes
7 answers

Python equivalent of golang's defer statement

How would one implement something that works like the defer statement from go in python? Defer pushes a function call to a stack. When the function containing the defer statement returns, the defered function calls are popped and executed one by…
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
39
votes
4 answers

Chained promises not passing on rejection

I am have a problem understanding why rejections are not passed on through a promise chain and I am hoping someone will be able to help me understand why. To me, attaching functionality to a chain of promises implies an intent that I am depending on…
Jordan
  • 711
  • 1
  • 6
  • 12
36
votes
2 answers

Difference between Job and Deferred in Coroutines Kotlin

I am new to coroutines, I understand launch and async but still confusing part is Deferred. What is Deferred? and difference between Job and Deferred. Clear explanation and example is more helpful. Thanks in advance.
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
1
2 3
75 76