Notes on Javascript Promises

Notes on Javascript Promises

Promises are not to be broken.

Collection of notes which trip me up when creating Javascript promises within Ajax requests.

Looping over an array to perform ajax requests, which are wrapped into promises:

The above creates an additional method on the javascript built-in Promise object adding a "thenReturn" method:

Promise.prototype.thenReturn = function(value) {
    return this.then(function() {
    return value; 

The return value maintains the index of the iteration. Note that their Ajax call (follow kink above) returns a promise. This is important, because:

"Whenever you create a promise in a then, return it - any promise you don't return will not be waited for outside." src

Thus, when using the .then() syntax, check to make sure that your asynchronous code is returning a promise, otherwise the the code will not wait for a promise (because there isn't one!). Solution: Make sure your asynchronous operations (such as an Ajax request) actually return a promise if you're wanting to wait upon it.


MDN Guide to Using Promises & Chaining

Collect recurring payments with Subscribie - Try Now