From c5a33776da41b7e97f8cbbea7ecd398053904e68 Mon Sep 17 00:00:00 2001 From: AliusDieMorietur Date: Sat, 30 Nov 2019 14:29:26 +0200 Subject: [PATCH] Composition task --- Exercises/2-compose.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Exercises/2-compose.js b/Exercises/2-compose.js index 368e521..9530138 100644 --- a/Exercises/2-compose.js +++ b/Exercises/2-compose.js @@ -1,5 +1,22 @@ 'use strict'; -const compose = (...fns) => x => null; +const compose = (...fns) => { + let error = null; + try { + for (const fn of fns) { + if (!(fn instanceof Function)) throw new Error('Type of argument should be a function.'); + } + } catch (e) { + error = e; + } + const composed = x => { + if (error) return undefined; + return fns.reduceRight((v, f) => f(v), x); + } + composed.on = (eventName, callback) => { + if (eventName === 'error' && error) callback(error); + } + return composed; +} module.exports = { compose }; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy