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
169
votes
8 answers

Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai

So, I'm testing a component that relies on an event-emitter. To do so I came up with a solution using Promises with Mocha+Chai: it('should transition with the correct event', (done) => { const cFSM = new CharacterFSM({}, emitter, transitions); …
Jzop
  • 1,735
  • 2
  • 11
  • 8
169
votes
6 answers

How to create an Observable from static data similar to http one in Angular?

I am having a service that has this method: export class TestModelService { public testModel: TestModel; constructor( @Inject(Http) public http: Http) { } public fetchModel(uuid: string = undefined): Observable { …
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
169
votes
13 answers

Promise - is it possible to force cancel a promise

I use ES6 Promises to manage all of my network data retrieval and there are some situations where I need to force cancel them. Basically the scenario is such that I have a type-ahead search on the UI where the request is delegated to the backend has…
Moonwalker
  • 3,462
  • 4
  • 25
  • 31
164
votes
1 answer

Futures vs. Promises

I'm confusing myself with difference between a future and a promise. Obviously, they have different methods and stuff, but what is the actual use case? Is it?: when I'm managing some async task, I use future to get the value "in future" when I'm…
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
162
votes
13 answers

Break promise chain and call a function based on the step in the chain where it is broken (rejected)

Update: To help future viewers of this post, I created this demo of pluma's answer. Question: My goal seems fairly straightforward. step(1) .then(function() { return step(2); }, function() { stepError(1); return $q.reject(); }) …
m59
  • 43,214
  • 14
  • 119
  • 136
161
votes
11 answers

JavaScript array .reduce with async/await

Seem to be having some issues incorporating async/await with .reduce(), like so: const data = await bodies.reduce(async(accum, current, index) => { const methodName = methods[index] const method = this[methodName] if (methodName == 'foo') { …
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
161
votes
4 answers

Node JS Promise.all and forEach

I have an array like structure that exposes async methods. The async method calls return array structures that in turn expose more async methods. I am creating another JSON object to store values obtained from this structure and so I need to be…
user3205931
160
votes
1 answer

What is the equivalent of Bluebird Promise.finally in native ES6 promises?

Bluebird offers a finally method that is being called whatever happens in your promise chain. I find it very handy for cleaning purposes (like unlocking a resource, hiding a loader, ...) Is there an equivalent in ES6 native promises?
Aric Lasry
  • 1,769
  • 3
  • 11
  • 11
159
votes
8 answers

How to make a promise from setTimeout

This is not a realworld problem, I'm just trying to understand how promises are created. I need to understand how to make a promise for a function that returns nothing, like setTimeout. Suppose I have: function async(callback){ …
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
156
votes
17 answers

Cancel a vanilla ECMAScript 6 Promise chain

Is there a method for clearing the .thens of a JavaScript Promise instance? I've written a JavaScript test framework on top of QUnit. The framework runs tests synchronously by running each one in a Promise. (Sorry for the length of this code…
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102
156
votes
3 answers

Why does javascript ES6 Promises continue execution after a resolve?

As I understand a promise is something that can resolve() or reject() but I was suprised to find out that code in the promise continues to execute after a resolve or reject is called. I considered resolve or reject being an async-friendly version of…
Ludwig Van Beethoven
  • 1,866
  • 2
  • 13
  • 12
155
votes
5 answers

How to use fetch in TypeScript

I am using window.fetch in Typescript, but I cannot cast the response directly to my custom type: I am hacking my way around this by casting the Promise result to an intermediate 'any' variable. What would be the correct method to do this? import {…
Kokodoko
  • 26,167
  • 33
  • 120
  • 197
154
votes
7 answers

Why can I not throw inside a Promise.catch handler?

Why can't I just throw an Error inside the catch callback and let the process handle the error as if it were in any other scope? If I don't do console.log(err) nothing gets printed out and I know nothing about what happened. The process just…
demian85
  • 2,394
  • 3
  • 20
  • 20
153
votes
8 answers

Handling multiple catches in promise chain

I am still fairly new to promises and am using bluebird currently, however I have a scenario where I am not quite sure how to best deal with it. So for example I have a promise chain within an express app like…
Grofit
  • 17,693
  • 24
  • 96
  • 176
153
votes
9 answers

Can promises have multiple arguments to onFulfilled?

I'm following the spec here and I'm not sure whether it allows onFulfilled to be called with multiple arguments. For example: promise = new Promise(function(onFulfilled, onRejected){ onFulfilled('arg1', 'arg2'); }) such that my…
badunk
  • 4,310
  • 5
  • 27
  • 47