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 };