Resolve promises one after another (i.e. in sequence)?

Consider the following code that reads an array of files in a serial/sequential manner. readFiles returns a promise, which is resolved only once all files have been read in sequence. var readFile = function(file) { … // Returns a promise. }; var readFiles = function(files) { return new Promise((resolve, reject) => { var readSequential = … Read more

Resolve Javascript Promise outside the Promise constructor scope

I have been using ES6 Promise. Ordinarily, a Promise is constructed and used like this new Promise(function(resolve, reject){ if (someCondition){ resolve(); } else { reject(); } }); But I have been doing something like below to take the resolve outside for the sake of flexibility. var outsideResolve; var outsideReject; new Promise(function(resolve, reject) { outsideResolve = … Read more

What is std::promise?

I’m fairly familiar with C++11’s std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward. However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The standard document itself doesn’t contain a whole lot of information beyond its class synopsis, and neither does std::thread. … Read more

Aren’t promises just callbacks?

I’ve been developing JavaScript for a few years and I don’t understand the fuss about promises at all. It seems like all I do is change: api(function(result){ api2(function(result2){ api3(function(result3){ // do work }); }); }); Which I could use a library like async for anyway, with something like: api().then(function(result){ api2().then(function(result2){ api3().then(function(result3){ // do work }); … Read more

jQuery deferreds and promises – .then() vs .done()

I’ve been reading about jQuery deferreds and promises and I can’t see the difference between using .then() & .done() for successful callbacks. I know Eric Hynds mentions that .done() and .success() map to the same functionality but I’m guessing so does .then() as all the callbacks are all invoked on a completion of a successful … Read more

Wait until all promises complete even if some rejected

Let’s say I have a set of Promises that are making network requests, of which one will fail: // http://does-not-exist will throw a TypeError var arr = [ fetch(‘index.html’), fetch(‘http://does-not-exist’) ] Promise.all(arr) .then(res => console.log(‘success’, res)) .catch(err => console.log(‘error’, err)) // This is executed Let’s say I want to wait until all of these have … Read more

JavaScript Promises – reject vs. throw

I have read several articles on this subject, but it is still not clear to me if there is a difference between Promise.reject vs. throwing an error. For example, Using Promise.reject return asyncIsPermitted() .then(function(result) { if (result === true) { return true; } else { return Promise.reject(new PermissionDenied()); } }); Using throw return asyncIsPermitted() .then(function(result) … Read more

What is the explicit promise construction antipattern and how do I avoid it?

I was writing code that does something that looks like: function getStuffDone(param) { | function getStuffDone(param) { var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) { // or = new $.Deferred() etc. | // using a promise constructor myPromiseFn(param+1) | myPromiseFn(param+1) .then(function(val) { /* or .done */ | .then(function(val) { … Read more

How do I access previous promise results in a .then() chain?

I have restructured my code to promises, and built a wonderful long flat promise chain, consisting of multiple .then() callbacks. In the end I want to return some composite value, and need to access multiple intermediate promise results. However the resolution values from the middle of the sequence are not in scope in the last … Read more