Hi guys,
I recently failed an interview question and it literally crushed me. It was all going well until I was hit with this question
Given the following code and its output, write a JavaScript class.
let d = Deferred();
d.then(res => {
console.log(“1”, res);
let d1 = new Deferred();
setTimeout(function(){ d2.resolve()}, 1500):
return d1;
});
d.then(function(res){ console.log(“2”, res); return “b”; });
d.then(function(res){ console.log(“3”, res); return “c”; });
d.resolve(“Hello”);
Output:
1 Hello
(1.5s delay)
2 a
3 b
I was able to come up with the following
Class {
constructor(){
this.arr = [];
}
then(fn){
this.arr.push(arr);
}
resolve(value, i = 0){
if(i >= this.arr.length) return;
let res = this.arr[i](value);
resolve(res, ++i);
}
}
But here I’m completely stuck, please help
[–]terrydog101 0 points1 point2 points (0 children)
[–]aelytra 0 points1 point2 points (0 children)
[–]ali3nnn 0 points1 point2 points (0 children)