UNPKG

@constantiner/fun-ctional

Version:

The library brings most of the familiar functional techniques (like functional composition) to asynchronous world with shining Promises

85 lines (77 loc) 3.33 kB
/** * @constantiner/fun-ctional * The library brings most of the familiar functional techniques (like functional composition) to asynchronous world with shining Promises * * @author Konstantin Kovalev <constantiner@gmail.com> * @version v0.6.6 * @link https://github.com/Constantiner/fun-ctional#readme * @date 30 May 2019 * * MIT License * * Copyright (c) 2018-2019 Konstantin Kovalev * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ (function(global, factory) { typeof exports === "object" && typeof module !== "undefined" ? (module.exports = factory()) : typeof define === "function" && define.amd ? define(factory) : ((global = global || self), (global.funCtional = factory())); })(this, function() { "use strict"; var customPromiseHandlingSupportSupport = Symbol.for("Custom Promise Handling Support"); var addCustomPromiseHandlingSupport = function addCustomPromiseHandlingSupport(fn, customVersion) { return (fn[customPromiseHandlingSupportSupport] = customVersion), fn; }; /** * Composable version of catch method for promises. * * It gets a value (a promise or not), resolves it and if resulting promise was rejected calls catch function. * * It allows to handle errors within acompose or apipe asynchronous composition chains to restore broken state etc. * * A sample with acompose: * * <pre><code>const resultOrFallback = await acompose(acatch(handleAndRecoverFn), canFailFn)(someInput);</code></pre> * * Standalone usage: * * <pre><code>const resultOrFallback = await acatch(handleAndRecoverFn)(requestDataAndReturnPromise());</code></pre> * * It is the same as the following: * * <pre><code>requestDataAndReturnPromise().catch(handleAndRecoverFn).then(resultOrFallback => console.log(resultOrFallback));</code></pre> * * @param {function} catchFn Is function to handle Promise's rejection. * @returns {any => Promise} A function which expects any value as input (Promise or not) and returns a Promise. */ var acatch = function(catchFn) { var handler = function handler(value) { return Promise.resolve(value).catch(catchFn); }; return addCustomPromiseHandlingSupport(handler, function(promise) { return promise.catch(catchFn); }); }; return acatch; }); //# sourceMappingURL=acatch.js.map