"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
function useAsync(execute, deps) {
    var _a = __read(react_1.useState(undefined), 2), value = _a[0], setValue = _a[1];
    var _b = __read(react_1.useState(undefined), 2), error = _b[0], setError = _b[1];
    var promise = react_1.useRef(undefined);
    react_1.useLayoutEffect(function () {
        value !== undefined && setValue(undefined);
        error !== undefined && setError(undefined);
        var p = execute();
        promise.current = p;
        p.then(function (result) { return promise.current === p && setValue(result); }, function (err) { return promise.current === p && setError(err); });
        return function () {
            promise.current = undefined;
        };
    }, deps);
    return [value, error];
}
exports.useAsync = useAsync;
//# sourceMappingURL=useAsync.js.map