I had an interview today, I cannot say who, and the question was a JavaScript Promise question I could not figure out.
Essentially you are given a class in which all the methods can be chained off of one another. The kicker is that each function takes any amount of time and that the next function needs to wait until the last function is executed to start.
The chaining is straightforward because you can just return a reference to the class, however the Promise
is what threw me off.
I had come up with the idea of a queue which make sense, but I still couldn't figure out how to fully complete a Promise
and then return something from the class method.
Can anyone explain, below is an example of the code.
Note: it does not work it is just an example of the sample code. this.resolvePromise();
is just a stub for what is supposed to happen.
const p = new Promise((resolve, reject) => {
resolve('Success!');
});
class X {
hello(){
console.log("Hellow");
this.resolvePromise();
return this;
}
bye(){
console.log("Bye");
this.resolvePromise();
return this
}
}
let x = new X();
x.hello().bye();