@constantiner/fun-ctional
Version:
The library brings most of the familiar functional techniques (like functional composition) to asynchronous world with shining Promises
92 lines (84 loc) • 3.71 kB
JavaScript
/**
* @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 promise.then(mapFn).catch(catchFn).
*
* It gets a value (a promise or not), resolves it and handles as promise.then(mapFn).catch(catchFn) returning resulting promise.
*
* 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(applySafe(canFailFn, handleAndRecoverFn), canFailTooFn)(someInput);</code></pre>
*
* Standalone usage:
*
* <pre><code>const resultOrFallback = await applySafe(canFailFn, handleAndRecoverFn)(requestDataAndReturnPromise());</code></pre>
*
* Or even:
*
* <pre><code>const resultOrFallback = await applySafe(acompose(handlerFn2, handlerFn1), handleAndRecoverFn)(requestDataAndReturnPromise());</code></pre>
*
* It is the same as the following:
*
* <pre><code>requestDataAndReturnPromise().then(canFailFn).catch(handleAndRecoverFn).then(resultOrFallback => console.log(resultOrFallback));</code></pre>
*
* @param {function} mapFn Is function to handle Promise's resolution (then).
* @param {function} catchFn Is function to handle Promise's rejection (catch).
* @returns {any => Promise} A function which expects any value as input (Promise or not) and returns a Promise.
*/
var applySafe = function(mapFn, catchFn) {
var handler = function handler(value) {
return Promise.resolve(value)
.then(mapFn)
.catch(catchFn);
};
return addCustomPromiseHandlingSupport(handler, function(promise) {
return promise.then(mapFn).catch(catchFn);
});
};
return applySafe;
});
//# sourceMappingURL=applySafe.js.map