"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = until;

var _setImmediate2 = require("./_setImmediate");

function until(test, task, callback) {
  var i = 0;

  function cb(err, res) {
    if (err || test(i)) {
      callback && callback(err, res);
    } else {
      (0, _setImmediate2._setImmediate)(function () {
        // prevent RangeError: Maximum call stack size exceeded for sync tasks
        run();
      });
    }
  }

  function run() {
    task(cb, i++);
  }

  if (!test(i)) {
    run();
  } else {
    callback && callback();
  }
}