googlearchive / TemplateBinding

TemplateBinding Prolyfill
290 stars 61 forks source link

ensure order when using the then method in unit tests #212

Closed pflannery closed 9 years ago

pflannery commented 9 years ago

would be really nice if we could have the then calls ordered so we can put break points in between them, currently using setTimeout breaks the tests when there is one break point in them. Would be nice to replace setTimeout with Promises. like this:

// chainable then method
function then(callback) {
  var queue = this.queue;
  if (!queue)
    queue = this.queue = [];

  queue.push(Platform.performMicrotaskCheckpoint);
  queue.push(callback);

  return {
    then: then.bind(this),
    run: function (doneCallback) {
      var runnerPromise = Promise.resolve();
      queue.forEach(function (queuedPromise) {
        runnerPromise = runnerPromise.then(queuedPromise);
      });
      if (doneCallback)
        runnerPromise.then(doneCallback);
    }
  };
}
// example usage:
  new then(function() {
    console.log(1);
  }).then(function() {
    console.log(2);
  }).then(function() {
    console.log(3)
  }).then(function() {
    console.log(3)
  }).run(function() {
    console.log("complete");
  });