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

How do I promisify native XHR?

I want to use (native) promises in my frontend app to perform XHR request but without all the tomfoolery of a massive framework. I want my xhr to return a promise but this doesn't work (giving me: Uncaught TypeError: Promise resolver undefined is…
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
227
votes
8 answers

Return from a promise then()

I have got a javascript code like this: function justTesting() { promise.then(function(output) { return output + 1; }); } var test = justTesting(); I have got always an undefined value for the var test. I think that it is because the…
Priscy
  • 2,303
  • 2
  • 10
  • 6
216
votes
4 answers

How do I wait for a promise to finish before returning the variable of a function?

I'm still struggling with promises, but making some progress thanks to the community here. I have a simple JS function which queries a Parse database. It's supposed to return the array of results, but obviously due to the asynchronous nature of the…
mac_55
  • 4,130
  • 7
  • 31
  • 40
204
votes
8 answers

How to unwrap the type of a Promise?

Say I have the following code: async promiseOne() { return 1 } // => Promise const promisedOne = promiseOne() typeof promisedOne // => Promised How would I go about extracting the type of the promise result (in this simplified…
fny
  • 31,255
  • 16
  • 96
  • 127
196
votes
8 answers

How to cancel an $http request in AngularJS?

Given a Ajax request in AngularJS $http.get("/backend/").success(callback); what is the most effective way to cancel that request if another request is launched (same backend, different parameters for instance).
mpm
  • 20,148
  • 7
  • 50
  • 55
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
195
votes
5 answers

Is it bad practice to have a constructor function return a Promise?

I'm trying to create a constructor for a blogging platform and it has many async operations going on inside. These range from grabbing the posts from directories, parsing them, sending them through template engines, etc. So my question is, would it…
adam-beck
  • 5,659
  • 5
  • 20
  • 34
184
votes
4 answers

How to wait for a JavaScript Promise to resolve before resuming function?

I'm doing some unit testing. The test framework loads a page into an iFrame and then runs assertions against that page. Before each test begins, I create a Promise which sets the iFrame's onload event to call resolve(), sets the iFrame's src, and…
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102
182
votes
8 answers

Using setTimeout on promise chain

Here i am trying to wrap my head around promises.Here on first request i fetch a set of links.and on next request i fetch the content of first link.But i want to make a delay before returning next promise object.So i use setTimeout on it. But it…
AL-zami
  • 8,902
  • 15
  • 71
  • 130
179
votes
6 answers

Angular HttpPromise: difference between `success`/`error` methods and `then`'s arguments

According to AngularJS doc, calls to $http return the following: Returns a promise object with the standard then method and two http specific methods: success and error. The then method takes two arguments a success and an error callback which will…
ejoubaud
  • 5,013
  • 6
  • 37
  • 39
176
votes
23 answers

typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here

I am trying to use Typescript for my AWS Lambda and i am getting the following errors where ever I use promises. error TS2693: 'Promise' only refers to a type, but is being used as a value here. I tried using the following variations in the code…
kalyanvgopal
  • 1,931
  • 2
  • 14
  • 13
175
votes
9 answers

How to promisify Node's child_process.exec and child_process.execFile functions with Bluebird?

I'm using the Bluebird promise library under Node.js, it's great! But I have a question: If you take a look at the documentation of Node's child_process.exec and child_process.execFile you can see that both of these functions are returning a…
Zoltan
  • 2,631
  • 2
  • 17
  • 13
174
votes
2 answers

Placement of catch BEFORE and AFTER then

I have trouble understanding the difference between putting .catch BEFORE and AFTER then in a nested promise. Alternative 1: test1Async(10).then((res) => { return test2Async(22) .then((res) => { return test3Async(100); }).catch((err)…
Zanko
  • 4,298
  • 4
  • 31
  • 54
172
votes
4 answers

How do I properly test promises with mocha and chai?

The following test is behaving oddly: it('Should return the exchange rates for btc_ltc', function(done) { var pair = 'btc_ltc'; shapeshift.getRate(pair) .then(function(data){ expect(data.pair).to.equal(pair); …
chovy
  • 72,281
  • 52
  • 227
  • 295
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