"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("core-js/modules/es6.promise");

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _default = function _default(f, ms) {
  var waiting = null;

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

  var debouncedThunk = (0, _debounce.default)(ready, ms);
  return function () {
    var promise = new Promise(function (resolve, reject) {
      return waiting = {
        resolve: resolve,
        reject: reject
      };
    });
    debouncedThunk.apply(void 0, arguments);
    return promise;
  };
};

exports.default = _default;