JSFiddle - React, Tailwind, and code Playground

by jfriend00

HTML

<script src="http://files.the-friend-family.com/log.js"></script>

JavaScript

function someFunction(i) {
    var d = $.Deferred();
    setTimeout(function() {
        log(i);
        d.resolve(i);
    }, 500)
    return d.promise();
}


var someArray = [1,2,3,4];

someArray.reduce(function(p, item) {
  return p.then(function(array) {
    return someFunction(item).then(function(val) {
        array.push(val);
        return array;
    });
  });
}, $.Deferred().resolve([])).then(function(array) {
   log(array)
  // all done here
  // accumulated results in array
}, function(err) {
    console.log(err)
  // err is the error from the rejected promise that stopped the chain of execution
});