From bc64412e791ad7e0c6ffa79766162e1d82c87270 Mon Sep 17 00:00:00 2001 From: Ivan Huz Date: Thu, 20 Feb 2025 19:00:04 +0200 Subject: [PATCH] Support chaining syntax --- JavaScript/9-thenable.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/JavaScript/9-thenable.js b/JavaScript/9-thenable.js index 5ef4694..c640e3e 100644 --- a/JavaScript/9-thenable.js +++ b/JavaScript/9-thenable.js @@ -18,11 +18,10 @@ class Thenable { resolve(value) { const fn = this.fn; if (fn) { - const next = fn(value); - if (next) { - next.then((value) => { - this.next.resolve(value); - }); + const result = fn(value); + const next = this.next; + if (next.fn) { + next.resolve(result); } } } @@ -39,8 +38,11 @@ const readFile = (filename) => { return thenable; }; +const MINIFY_REGEX = new RegExp('\\s+', 'g'); +const minify = (content) => content.replaceAll(MINIFY_REGEX, ''); + const main = async () => { - const file1 = await readFile('9-thenable.js'); + const file1 = await readFile('9-thenable.js').then(minify); console.dir({ length: file1.length }); };