Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions JavaScript/9-thenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ class Thenable {
return next;
}

resolve(value) {
async resolve(value) {
const fn = this.fn;
if (fn) {
const next = fn(value);
if (next) {
next.then(value => {
this.next.resolve(value);
});
const next = await fn(value);
if (this.next) {
this.next.resolve(next);
}
}
}
Expand All @@ -39,9 +37,26 @@ const readFile = filename => {
return thenable;
};

const delay = fn => (...args) => {
const thenable = new Thenable();
setTimeout(thenable.resolve.bind(thenable), 1000, fn(...args));
return thenable;
};

const fn = val => val;
const mul = val => val * 5;
const add = val => val + 2;

const fnDel = delay(fn);
const mulDel = delay(mul);
const addDel = delay(add);

(async () => {

const file1 = await readFile('9-thenable.js');
console.dir({ length: file1.length });

const res = await fnDel(7).then(mulDel).then(add).then(addDel);
console.log(res);

})();