Let's say I have a class Test
with around 10-20 methods, all of which are chainable.
In another method, I have some asynchronous work to do.
let test = new Test();
console.log(test.something()); // Test
console.log(test.asynch()); // undefined since the async code isn't done yet
console.log(test.asynch().something()); // ERROR > My goal is to make this
Since every other method is chainable, I feel like it would be weird for the user if this sole method isn't.
Is there a way for me to maintain the chainable theme of my Class?
I have already thought of passing the next method in a callback function inside this method's parameter, but it's not really chaining.
test.asynch(() => something())
Same thing with Promises
, it's not really chaining.
test.asynch().then(() => something())
The result I want is
test.asynch().something()
Here is a snippet that demonstrates my issue :
class Test {
/**
* Executes some async code
* @returns {Test} The current {@link Test}
*/
asynch() {
if (true) { //Condition isn't important
setTimeout(() => { //Some async stuff
return this;
}, 500);
} else {
// ...
return this;
}
}
/**
* Executes some code
* @returns {Test} The current {@link Test}
*/
something() {
// ...
return this
}
}
let test = new Test();
console.log(test.something()); // Test
console.log(test.asynch()); // undefined
console.log(test.asynch().something()); // ERROR > My goal is to make this work.