'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _debounce = require('lodash/debounce');

var _debounce2 = _interopRequireDefault(_debounce);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = function (f, ms) {
  var waiting = null;

  var ready = function ready() {
    var old = waiting;
    f.apply(undefined, arguments).then(function (r) {
      return old.resolve(r);
    }).catch(function (e) {
      return old.reject(e);
    });
    waiting = null;
  };

  var debouncedThunk = (0, _debounce2.default)(ready, ms);

  return function () {
    var promise = new Promise(function (resolve, reject) {
      return waiting = { resolve: resolve, reject: reject };
    });
    debouncedThunk.apply(undefined, arguments);
    return promise;
  };
};