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
403
votes
6 answers

Do I need to return after early resolve/reject?

Suppose I have the following code. function divide(numerator, denominator) { return new Promise((resolve, reject) => { if(denominator === 0){ reject("Cannot divide by 0"); return; //superfluous? } resolve(numerator / denominator); …
sam
  • 4,033
  • 2
  • 10
  • 4
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
371
votes
9 answers

What's the difference between a Future and a Promise?

What's the difference between Future and Promise? They both act like a placeholder for future results, but where is the main difference?
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
323
votes
14 answers

Axios handling errors

I'm trying to understand javascript promises better with Axios. What I pretend is to handle all errors in Request.js and only call the request function from anywhere without having to use catch(). In this example, the response to the request will be…
mignz
  • 3,648
  • 2
  • 20
  • 21
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
305
votes
3 answers

Promise.all: Order of resolved values

Looking at MDN it looks like the values passed to the then() callback of Promise.all contains the values in the order of the promises. For example: var somePromises = [1, 2, 3, 4, 5].map(Promise.resolve); return…
Thorben Croisé
  • 12,407
  • 8
  • 39
  • 50
300
votes
14 answers

Is Node.js native Promise.all processing in parallel or sequentially?

I would like to clarify this point, as the documentation is not too clear about it; Q1: Is Promise.all(iterable) processing all promises sequentially or in parallel? Or, more specifically, is it the equivalent of running chained promises like…
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
275
votes
9 answers

Why is my asynchronous function returning Promise { } instead of a value?

My code: let AuthUser = data => { return google.login(data.username, data.password).then(token => { return token } ) } And when i try to run something like this: let userToken = AuthUser(data) console.log(userToken) I'm getting: Promise {…
Src
  • 5,252
  • 5
  • 28
  • 56
269
votes
14 answers

How can I access the value of a promise?

I'm looking at this example from Angular's documentation for $q, but I think this probably applies to promises in general. The example below is copied verbatim from their documentation with their comment included: promiseB =…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
254
votes
6 answers

Difference between microtask and macrotask within an event loop context

I've just finished reading the Promises/A+ specification and stumbled upon the terms microtask and macrotask: see http://promisesaplus.com/#notes I've never heard of these terms before, and now I'm curious what the difference could be? I've already…
NicBright
  • 7,289
  • 5
  • 18
  • 19
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
241
votes
31 answers

How can I synchronously determine a JavaScript Promise's state?

I have a pure JavaScript Promise (built-in implementation or poly-fill): var promise = new Promise(function (resolve, reject) { /* ... */ }); From the specification, a Promise can be one of: 'settled' and 'resolved' 'settled' and…
jokeyrhyme
  • 3,258
  • 2
  • 20
  • 20
236
votes
6 answers

Why does .json() return a promise?

I've been messing around with the fetch() api recently, and noticed something which was a bit quirky. let url = "http://jsonplaceholder.typicode.com/posts/6"; let iterator = fetch(url); iterator .then(response => { return { …
haveacigaro
  • 2,509
  • 2
  • 13
  • 8
233
votes
7 answers

When is .then(success, fail) considered an antipattern for promises?

I had a look at the bluebird promise FAQ, in which it mentions that .then(success, fail) is an antipattern. I don't quite understand its explanation as for the try and catch. What's wrong with the following? some_promise_call() .then(function(res) {…
user2127480
  • 4,623
  • 5
  • 19
  • 17
229
votes
5 answers

How to find which promises are unhandled in Node.js UnhandledPromiseRejectionWarning?

Node.js from version 7 has async/await syntactic sugar for handling promises and now in my code the following warning comes up quite often: (node:11057) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1):…
user1658162
  • 2,721
  • 2
  • 19
  • 23