1

I have several scenarios in my code that fit this pattern, and I'm sure its a common programming issue. I want to loop through an array or an object, and run a (potentially) asynchronous function on each iteration. I do not want the next iteration to start until the current one has finished.

Here's the code that would work if the processing was synchronous

    var k;
    for (k in data) {
        var thisdata = data[k];
        dosomething(thisdata);
    }

In the above, dosomething might visit the server, or it might throw up a dialog box for user input, or it might just do some local processing in js.

Here's the solution I've come up with:

    var keys = Object.keys(data);
    var index = 0;
    exec();

    function exec() {
        if (index == keys.length) return;
        var k = keys[index++];
        var thisdata = data[k];
        dosomething(thisdata, exec);
    }

So dosomething() had to be modified to take a callback, which is fine, but using Object.keys and having such inelegant and difficult to read code seems wrong. So is there an easier/better way ?

Also there could potentially be thousands of iterations of this loop, so "stackoverflow" is a concern, I suppose, ie. having a 1000 deep recursion going on.

user2728841
  • 1,333
  • 17
  • 32
  • Don't use `for ... in` for iteration through arrays. – Yeldar Kurmangaliyev Nov 11 '15 at 06:09
  • 3
    i think recursion is the right way to do this – Rana Ahmer Yasin Nov 11 '15 at 06:12
  • 1
    Promises might also be a good idea. – Kodz Nov 11 '15 at 06:14
  • Thanks, I'd like to see a "promises" version of this. I've not used promises before. (I'll get googling it in the meantime!) – user2728841 Nov 11 '15 at 06:17
  • In fact, [a stack overflow is not a concern if the function is really asynchronous](http://stackoverflow.com/q/29456460/1048572) – Bergi Nov 11 '15 at 06:18
  • 1
    @Ahmer recursion won't solve this – Jaromanda X Nov 11 '15 at 06:18
  • 1
    @user2728841: [Just one simple promise example](http://stackoverflow.com/a/23650478/1048572) (there are thousands :-)) – Bergi Nov 11 '15 at 06:19
  • @JaromandaX: Not sure what you're talking about. The OP already showed a working, recursive approach in the question. So what is not solved? – Bergi Nov 11 '15 at 06:20
  • Most of the function calls will not be async, but some will be. Its not for node, its a client language interpreter for an enterprise app, running in the browser. recursion is what we have at the moment isn't it ? (in my proposed solution) – user2728841 Nov 11 '15 at 06:20
  • Thanks for the promises example. Its a bit cryptic but I'll try to work it out ! – user2728841 Nov 11 '15 at 06:26
  • @Bergi, re the recursion link that stack overflow is not a concern, would the caller not stay in memory since the callee has access to all its variables and functions? – user2728841 Nov 11 '15 at 06:38
  • 1
    @user2728841: no, the callee does not have access to the caller scope, unless its a closure. And the point about asynchronous invocation is that your callback is called not by some "caller" (or even the function that you called), but by the event loop, with a whole new stack. – Bergi Nov 11 '15 at 06:46
  • @Bergi - I based my comment on the last concern in the question – Jaromanda X Nov 11 '15 at 07:37

3 Answers3

2

If you have the possibility of using a transpiler, I would suggest you to check the ES7 async/await proposal. You can code like this, and then tranpile it through Babel (or Facebook Regenerator):

const doSomething = (param) => {
  return new Promise((resolve, reject) => {
    // in my example, I'll do an AJAX request
    const xhr = new XMLHttpRequest();
    xhr.addEventListener('load', (res) => {
      if (xhr.status >= 200 && xhr.status < 300)
        resolve(JSON.parse(xhr.responseText));
      else
        reject(xhr.statusText);
    });
    xhr.addEventListener('error', (err) => {
      reject(err);
    });
    xhr.open('GET', 'https://api.github.com/repos/buzinas/tslint-eslint-rules/issues/' + param);
    xhr.send();
  });
}

const getData = async (data) => { 
  for (let d of data) {
    let result = await doSomething(d);
    document.querySelector('ul').insertAdjacentHTML('beforeend', `<li>${d}: ${result.title} - ${result.state}</li>`);
  }
};

getData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

Take a look at the transpiled code running below:

'use strict';

var _this = this;

var doSomething = function doSomething(param) {
  return new Promise(function (resolve, reject) {
    // in my example, I'll do an AJAX request
    var xhr = new XMLHttpRequest();
    xhr.addEventListener('load', function (res) {
      if (xhr.status >= 200 && xhr.status < 300) resolve(JSON.parse(xhr.responseText));else reject(xhr.statusText);
    });
    xhr.addEventListener('error', function (err) {
      reject(err);
    });
    xhr.open('GET', 'https://api.github.com/repos/buzinas/tslint-eslint-rules/issues/' + param);
    xhr.send();
  });
};

var getData = function getData(data) {
  var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, d, result;

  return regeneratorRuntime.async(function getData$(context$1$0) {
    while (1) switch (context$1$0.prev = context$1$0.next) {
      case 0:
        _iteratorNormalCompletion = true;
        _didIteratorError = false;
        _iteratorError = undefined;
        context$1$0.prev = 3;
        _iterator = data[Symbol.iterator]();

      case 5:
        if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
          context$1$0.next = 14;
          break;
        }

        d = _step.value;
        context$1$0.next = 9;
        return regeneratorRuntime.awrap(doSomething(d));

      case 9:
        result = context$1$0.sent;

        document.querySelector('ul').insertAdjacentHTML('beforeend', '<li>' + d + ': ' + result.title + ' - ' + result.state + '</li>');

      case 11:
        _iteratorNormalCompletion = true;
        context$1$0.next = 5;
        break;

      case 14:
        context$1$0.next = 20;
        break;

      case 16:
        context$1$0.prev = 16;
        context$1$0.t0 = context$1$0['catch'](3);
        _didIteratorError = true;
        _iteratorError = context$1$0.t0;

      case 20:
        context$1$0.prev = 20;
        context$1$0.prev = 21;

        if (!_iteratorNormalCompletion && _iterator['return']) {
          _iterator['return']();
        }

      case 23:
        context$1$0.prev = 23;

        if (!_didIteratorError) {
          context$1$0.next = 26;
          break;
        }

        throw _iteratorError;

      case 26:
        return context$1$0.finish(23);

      case 27:
        return context$1$0.finish(20);

      case 28:
      case 'end':
        return context$1$0.stop();
    }
  }, null, _this, [[3, 16, 20, 28], [21,, 23, 27]]);
};

getData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.33/browser-polyfill.min.js"></script>

<ul>

</ul>
Buzinas
  • 11,597
  • 2
  • 36
  • 58
  • This has the potential to be the solution I end up using. Just doing lots of learning about all this at themoment.. thanks :) – user2728841 Nov 14 '15 at 10:37
1

Here is an example using async. The code runs an asynchronous function on any even number in the data.

var async = require('async');

var data = [10, 11, 22, 33, 14, 11, 6, 57, 8, 49];

// Waits 2 seconds then prints the data (async)
// Notice the added parameter for the callback
function doSomething(data, done) {
    setTimeout(function() {
        console.log('async', data);
        done();
    }, 2000);
}

// Go through each member of the data
async.each(data, function(thisdata, callback) {

    // Make async call if the number is even
    // Replace this with whatever logic you have for potentially running the async function
    if (thisdata % 2 === 0) {

        // Pass the data and the callback!
        doSomething(thisdata, callback);
    } 

    // Else make the callback right away
    else {
        callback();
    }
}, 
function() {
    console.log('all done');
});
Tyler
  • 17,669
  • 10
  • 51
  • 89
1

Here's a generic function with no external libs (except Promise Internet Exploder) - of course, Promises can be added to internet exploder with a polyfill such as https://www.promisejs.org/polyfills/promise-7.0.4.js

function inSeries(array, fn) {
    var promises = [];
    array.reduce(function(ret, v) { 
        var promise = ret.then(fn.bind(null, v)).then(
            function(x) { return {state: "fulfilled", value: x};}, 
            function(e) { return {state: "rejected" , value: e};}
        );
        promises.push(promise);
        return promise;
    }, Promise.resolve());
    return Promise.all(promises);
}

pass in an array of values, and a function that accepts a single argument (i.e each, values from the array)

The function can return a Promise, or a value, no matter

The returned promise is an array of results for each iteration of the function, in the form

{state: 'fulfilled or rejected', value: 'result or error'}

If, as you have in the question, you're "source" data is not an array, you can call the function like

inSeries(Object.keys(theData).map(function(key) {
    return theData[key];
}), someFunction).then( //etc

Here's a demo - http://jsfiddle.net/p5ojdhLc/

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • You said `I'd like to see a "promises" version of this` - adding Promises to Internet Exploder is too easy - https://www.promisejs.org/polyfills/promise-7.0.4.js - I use that in a production site because one of our clients insists on using internet exploder – Jaromanda X Nov 11 '15 at 08:59
  • Yes when I paste that code into a fiddle it does work in IE. – user2728841 Nov 11 '15 at 09:29