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
150
votes
9 answers

Understanding promises in Node.js

From what I have understood there are three ways of calling asynchronous code: Events, e.g. request.on("event", callback); Callbacks, e.g. fs.open(path, flags, mode, callback); Promises I found the node-promise library but I don’t get it. Could…
ajsie
  • 77,632
  • 106
  • 276
  • 381
142
votes
6 answers

How to return many Promises and wait for them all before doing other stuff

I have a loop which calls a method that does stuff asynchronously. This loop can call the method many times. After this loop, I have another loop that needs to be executed only when all the asynchronous stuff is done. So this illustrates what I…
Ganbin
  • 2,004
  • 2
  • 12
  • 19
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
140
votes
2 answers

How do I await multiple promises in-parallel without 'fail-fast' behavior?

I'm using async/await to fire several api calls in parallel: async function foo(arr) { const results = await Promise.all(arr.map(v => { return doAsyncThing(v) })) return results } I know that, unlike loops, Promise.all executes…
Brandon
  • 7,736
  • 9
  • 47
  • 72
139
votes
19 answers

How to include multiple js files using jQuery $.getScript() method

I am trying to dynamically include javascript files into my js file. I did some research about it and find jQuery $.getScript() method would be a desired way to go. // jQuery $.getScript('/path/to/imported/script.js', function() { // script is…
sozhen
  • 7,627
  • 14
  • 36
  • 53
135
votes
5 answers

What is the difference between JavaScript promises and async await?

I have been using ECMAScript 6 and ECMAScript 7 features already (thanks to Babel) in my applications - both mobile and web. The first step obviously was to ECMAScript 6 levels. I learnt many async patterns, the promises (which are really…
bozzmob
  • 12,364
  • 16
  • 50
  • 73
133
votes
6 answers

Correct Try...Catch Syntax Using Async/Await

I like the flatness of the new Async/Await feature available in Typescript, etc. However, I'm not sure I like the fact that I have to declare the variable I'm awaiting on the outside of a try...catch block in order to use it later. Like so: let…
freedomflyer
  • 2,431
  • 3
  • 26
  • 38
133
votes
1 answer

Promise : then vs then + catch

Is there any difference between the 2 followings codes ? myPromise.then(function() { console.log('success'); }).catch(function() { console.log('error'); }); myPromise.then(function() { console.log('success'); }, function() { …
Magus
  • 14,796
  • 3
  • 36
  • 51
132
votes
2 answers

Promise.resolve vs new Promise(resolve)

I'm using bluebird and I see two ways to resolve synchronous functions into a Promise, but I don't get the differences between both ways. It looks like the stacktrace is a little bit different, so they aren't just an alias, right? So what is the…
Pipo
  • 5,623
  • 7
  • 36
  • 46
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
126
votes
5 answers

TypeError: Cannot read property 'then' of undefined

loginService.islogged() Above function return a string like "failed". However, when I try to run then function on it, it will return error of TypeError: Cannot read property 'then' of undefined and the cursor is indicate right after connected and…
ey dee ey em
  • 7,991
  • 14
  • 65
  • 121
125
votes
5 answers

Can you resolve an angularjs promise before you return it?

I am trying to write a function that returns a promise. But there are times when the information requested is available immediately. I want to wrap it in a promise so that the consumer doesn't need to make a decision. function getSomething(id) { …
Craig Celeste
  • 12,207
  • 10
  • 42
  • 49
121
votes
6 answers

Rethrowing error in promise catch

I found the following code in a tutorial: promise.then(function(result){ //some code }).catch(function(error) { throw(error); }); I'm a bit confused: does the catch call accomplish anything? It seems to me that it doesn't have any effect,…
Tyler Durden
  • 2,031
  • 4
  • 20
  • 25
120
votes
13 answers

Correct way to write loops for promise.

How to correctly construct a loop to make sure the following promise call and the chained logger.log(res) runs synchronously through iteration? (bluebird) db.getUser(email).then(function(res) { logger.log(res); }); // this is a promise I tried the…
user2127480
  • 4,623
  • 5
  • 19
  • 17
119
votes
6 answers

Promises, pass additional parameters to then chain

A promise, just for example: var P = new Promise(function (resolve, reject) { var a = 5; if (a) { setTimeout(function(){ resolve(a); }, 3000); } else { reject(a); } }); After we call the .then() method on the…
user3110667
  • 1,201
  • 2
  • 9
  • 4