From b06c24de9fcbc59a04c0d0fc61b5efae9c086538 Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Wed, 3 Dec 2025 12:36:50 +0500 Subject: [PATCH 1/3] do 1-thenable --- JavaScript/Tasks/1-thenable.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/JavaScript/Tasks/1-thenable.js b/JavaScript/Tasks/1-thenable.js index 8adc643..9c78cba 100644 --- a/JavaScript/Tasks/1-thenable.js +++ b/JavaScript/Tasks/1-thenable.js @@ -2,11 +2,13 @@ // Task: change `iterate` contract from callbacks to Thenable -const iterate = (items, callback) => { - for (const item of items) { - callback(item); - } -}; +const iterate = (items) => ({ + then(resolve) { + for (const item of items) { + resolve(item); + } + }, +}); const electronics = [ { name: 'Laptop', price: 1500 }, @@ -14,7 +16,7 @@ const electronics = [ { name: 'HDMI cable', price: 10 }, ]; -// Use await syntax to get items -iterate(electronics, (item) => { - console.log(item); -}); +iterate(electronics).then(item=>console.log(item)) + + + From 7b8c584ea13af98222562ebae4976089d23635f0 Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Wed, 3 Dec 2025 12:43:28 +0500 Subject: [PATCH 2/3] do 2-reject --- JavaScript/Tasks/2-reject.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/JavaScript/Tasks/2-reject.js b/JavaScript/Tasks/2-reject.js index 77867f9..573db1e 100644 --- a/JavaScript/Tasks/2-reject.js +++ b/JavaScript/Tasks/2-reject.js @@ -5,10 +5,12 @@ const iterate = (items) => { let index = 0; return { - then(fulfill /*reject*/) { + then(fulfill, reject) { // Call both: fulfill and reject if (index < items.length) { fulfill(items[index++]); + } else { + reject(new Error('No items')) } }, }; From 99cb0a643b7cf3bd0b34ab5f1285980c131ff86c Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Wed, 3 Dec 2025 12:49:06 +0500 Subject: [PATCH 3/3] add 3 task --- JavaScript/Tasks/3-class.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/JavaScript/Tasks/3-class.js b/JavaScript/Tasks/3-class.js index c3d7839..b104ad1 100644 --- a/JavaScript/Tasks/3-class.js +++ b/JavaScript/Tasks/3-class.js @@ -3,16 +3,21 @@ // Task: rewrite to `class Iterator` implementing // Thenable contract with private fields. -const iterate = (items) => { - let index = 0; - return { - then(fulfill /*reject*/) { - if (index < items.length) { - fulfill(items[index++]); +class Iterator { + #items; + constructor(items){ + this.#items = items; + } + + then(fulfill /*reject*/) { + const item = this.#items.shift() + if (item) { + fulfill(item); } - }, - }; -}; + } +} + + const electronics = [ { name: 'Laptop', price: 1500 }, @@ -21,7 +26,7 @@ const electronics = [ ]; (async () => { - const items = iterate(electronics); + const items = new Iterator(electronics); // Use `new Iterator(electronics)` const item1 = await items; console.log(item1);