UNPKG

@rimbu/common

Version:

Common types and objects used in many other Rimbu packages

62 lines 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncOptLazy = void 0; var tslib_1 = require("tslib"); var AsyncOptLazy; (function (AsyncOptLazy) { /** * Returns the value or promised value contained in an `AsyncOptLazy` instance of type T. * @param optLazy - the `AsyncOptLazy` value of type T * @param args - when needed, the extra arguments to pass to the lazy invocation * @typeparam T - the value type * @typeparam A - (default: []) types of the argument array that can be passed in the lazy case * @example * ```ts * AsyncOptLazy.toMaybePromise(1) // => 1 * AsyncOptLazy.toMaybePromise(() => 1) // => 1 * AsyncOptLazy.toMaybePromise(() => () => 1) // => () => 1 * AsyncOptLazy.toMaybePromise(async () => 1) // => Promise(1) * AsyncOptLazy.toMaybePromise(Promise.resolve(1)) // => Promise(1) * ``` */ function toMaybePromise(optLazy) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } if (optLazy instanceof Function) return optLazy.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(args), false)); return optLazy; } AsyncOptLazy.toMaybePromise = toMaybePromise; /** * Returns the value contained in an `AsyncOptLazy` instance of type T as a promise. * @param optLazy - the `AsyncOptLazy` value of type T * @param args - when needed, the extra arguments to pass to the lazy invocation * @typeparam T - the value type * @typeparam A - (default: []) types of the argument array that can be passed in the lazy case * @example * ```ts * AsyncOptLazy.toPromise(1) // => Promise(1) * AsyncOptLazy.toPromise(() => 1) // => Promise(1) * AsyncOptLazy.toPromise(() => () => 1) // => Promise(() => 1) * AsyncOptLazy.toPromise(async () => 1) // => Promise(1) * AsyncOptLazy.toPromise(Promise.resolve(1)) // => Promise(1) * ``` */ function toPromise(optLazy) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { if (optLazy instanceof Function) return [2 /*return*/, optLazy.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(args), false))]; return [2 /*return*/, optLazy]; }); }); } AsyncOptLazy.toPromise = toPromise; })(AsyncOptLazy || (exports.AsyncOptLazy = AsyncOptLazy = {})); //# sourceMappingURL=async-optlazy.cjs.map