How can I effectively execute a series of asynchronous and synchronous functions in a specific order, ensuring they run one after the other, prior to invoking methods from a class in JavaScript? I'm looking for a clear approach to managing both types of functions in a sequential manner, enhancing the predictability and control of their execution flow before interacting with class methods.
I used proxies, but the problem with proxies is that if we want to use a proxied method in another class, the promise will be returned - also apply and get cannot be executed async.
Code
class TargetClass {
constructor() {
TargetClass['prototype']['someMethod'] = new Proxy(TargetClass['prototype']['someMethod'], proxyHandler);
TargetClass['prototype']['otherMethod'] = new Proxy(TargetClass['prototype']['otherMethod'], proxyHandler);
}
someMethod() {
const otm = this.otherMethod()
console.log(otm instanceof Promise , otm);
}
otherMethod(){
return 'other Methods'
}
}
async function concurrentFunction1() {
const test = await concurrentFunction2();
console.log(test);
}
async function concurrentFunction2() {
return "Result from concurrentFunction2";
}
const proxyHandler = {
apply: async function(target, thisArg, argumentsList) {
const result = await concurrentFunction1();
return Reflect.apply(target, thisArg, argumentsList);
}
};
(async () => {
const targetInstance = new TargetClass();
await targetInstance.someMethod()
})();
the output of the above code is
Result from concurrentFunction2
Promise { <pending> }
Result from concurrentFunction2
and why when two proxiedMethod use in each other one of them return promise?