"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.promise = exports.tryPromise = exports.tryCatchPromise = void 0;
const Function_1 = require("../Function");
const core_1 = require("./core");
const die_1 = require("./die");
const effectAsync_1 = require("./effectAsync");
const fail_1 = require("./fail");
/**
 * Create an Effect that when executed will construct `promise` and wait for its result,
 * errors will be handled using `onReject`
 */
function tryCatchPromise(promise, onReject, __trace) {
    return (0, effectAsync_1.effectAsync)((resolve) => {
        promise()
            .then((x) => (resolve((0, core_1.succeed)(x))))
            .catch((x) => (resolve((0, fail_1.fail)(onReject(x)))));
    }, __trace);
}
exports.tryCatchPromise = tryCatchPromise;
/**
 * Create an Effect that when executed will construct `promise` and wait for its result,
 * errors will produce failure as `unknown`
 */
function tryPromise(effect, __trace) {
    return (0, effectAsync_1.effectAsync)((resolve) => {
        effect()
            .then((x) => (resolve((0, core_1.succeed)(x))))
            .catch((x) => (resolve((0, fail_1.fail)(x))));
    }, __trace);
}
exports.tryPromise = tryPromise;
/**
 * Like tryPromise but produces a defect in case of errors
 */
function promise(effect, __trace) {
    return (0, effectAsync_1.effectAsync)((resolve) => {
        effect()
            .then((x) => (resolve((0, core_1.succeed)(x))))
            .catch((x) => (resolve((0, die_1.die)(x))));
    }, __trace);
}
exports.promise = promise;
//# sourceMappingURL=promise.js.map