Questions tagged [es6-promise]

An ES6 Promise is an ECMAScript 2015 object that represents an in-progress asynchronous operation

An ES6 Promise is an ECMAScript 2015 (ES6) object that represents the eventual completion or failure of an asynchronous operation. The ES6 Promise API is based on the Promises/A+ specification.

Consumer-side, a promise is essentially a returned object to attach callbacks to, instead of passing callbacks into a function. The .then function lets callers chain operations to be run subsequently.

In an ideal world, all asynchronous functions would return promises. However, a promise may also be created from scratch using its constructor, to wrap old callback-style function calls.

Promise objects have three possible states:

  • Pending
  • Fulfilled
  • Rejected

Resources

Related Tages

5243 questions
195
votes
8 answers

How to pass parameter to a promise function

this might seem a silly question but I am a newbie in this topic. I am working on promises on node js. And I want to pass parameter to a promise function. However I could not figure it out. someModule.someFunction.then(username,…
kundante
  • 2,100
  • 2
  • 16
  • 20
171
votes
5 answers

Returning Promises from Vuex actions

I recently started migrating things from jQ to a more structured framework being VueJS, and I love it! Conceptually, Vuex has been a bit of a paradigm shift for me, but I'm confident I know what its all about now, and totally get it! But there exist…
Daniel Park
  • 3,903
  • 4
  • 23
  • 38
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
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
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
136
votes
9 answers

Intellij Idea warning - "Promise returned is ignored" with aysnc/await

I'm using Express.js in my code with Node.js v7.3. In this I've created a User Router which forwards the requests to my User Controller. I'm using async/await inside the User Controller to do asynchronous calls. The problem is that IntelliJ gives me…
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
132
votes
10 answers

how to cancel/abort ajax request in axios

I use axios for ajax requests and reactJS + flux for render UI. In my app there is third side timeline (reactJS component). Timeline can be managed by mouse's scroll. App sends ajax request for the actual data after any scroll event. Problem that…
Rajab Shakirov
  • 7,265
  • 7
  • 28
  • 42
126
votes
9 answers

How to use Typescript with native ES6 Promises

I'm a complete beginner to Typescript and am wondering if it's possible to use ES6 promises in Typescript and what I would have to do to get them to work. I'm running node 0.11.14 and am getting an error during compilation "Cannot find name…
dchang
  • 2,440
  • 4
  • 24
  • 27
123
votes
12 answers

fs.writeFile in a promise, asynchronous-synchronous stuff

I need some help with my code. I'm new at Node.js and have a lot of trouble with it. What I'm trying to do: Fetch a .txt with Amazon products (ASINs) ; Fetch all products using the amazon-product-api package; Save each product in a .json…
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
113
votes
6 answers

Creating a (ES6) promise without starting to resolve it

Using ES6 promises, how do I create a promise without defining the logic for resolving it? Here's a basic example (some TypeScript): var promises = {}; function waitFor(key: string): Promise { if (key in promises) { return promises[key]; …
Barguast
  • 5,926
  • 9
  • 43
  • 73
109
votes
4 answers

When is the body of a Promise constructor callback executed?

Suppose I have the following code constructing a Promise: function doSomethingAsynchronous() { return new Promise((resolve) => { const result = doSomeWork(); setTimeout(() => { resolve(result); }), 100); }); } At which point…
Kevin
  • 14,655
  • 24
  • 74
  • 124
109
votes
5 answers

What happens if you don't resolve or reject a promise?

I have a scenario where I am returning a promise. The promise is basically triggered by an ajax request. On rejecting the promise it shows an error dialog that there is a server error. What I want to do is when the response code is 401, I neither…
Aniket
  • 4,926
  • 12
  • 41
  • 54
108
votes
4 answers

Differences between Promise.all() and Promise.allSettled() in JS?

I was reading the MDN's manual on Promise, and I found these two methods which seem similar to me: Promise.allSettled(iterable); Promise.all(iterable); Both of them take an iterable and return an array containing the fulfilled Promises. So, what…
ezio4df
  • 3,541
  • 6
  • 16
  • 31