I've got a class that returns several methods to be chained like this:
class MyClass{
foo(){
if(condition) return this;
else throw;
}
bar(){
if(condition) return this;
else throw;
}
}
Conditions are certain checks that may be true or false depending on the internal state of the class
The idea is that I can execute several methods like this:
myInstance.foo().bar();
This way, if any of the conditions in any method fails, this statement will throw an error.
However, now I'm trying to check conditions that require a promise to be resolved:
class MyClass2{
asynCheck(){
myPromise.then((condition)=>{
... // returning a promise won't work
});
I want to maintain the same syntax as before (adding an await/then), I don't mind changing foo and bar to be async as well, as long as the following syntax is valid:
await myInstance.foo().asyncCheck().bar();
Is there a way of doing this? Note that there may be more than one asyncCheck and may be called in any order