-2

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?

  • 2
    It's kind of confusing what you mean by "prior to invoking methods from a class". Why can't you just use async/await and call the methods one by one? – jeffkmeng Aug 26 '23 at 09:18
  • without actual code, it's hard to tell what you're doing wrong – Jaromanda X Aug 26 '23 at 09:32
  • updated Post - please check the code ! @jeffkmeng – reza salari Aug 26 '23 at 09:48
  • @jaromanda-x updated Post - please check the code ! – reza salari Aug 26 '23 at 09:49
  • [Don't overwrite prototype methods from inside the constructor!](https://stackoverflow.com/questions/28255957/assigning-prototype-methods-inside-the-constructor-function-why-not) Although really there is no reason to use proxies here at all. – Bergi Aug 26 '23 at 12:44

1 Answers1

0

Define function first as below:

function syncFunction() {
  console.log('Synchronous function executed');
}

async function asyncFunction() {
  console.log('Asynchronous function executed');
}

Below Class Method:

class MyClass {
  constructor() {}

  async init() {
    await this.executeSyncAndAsync();
    this.invokeClassMethods();
  }

  async executeSyncAndAsync() {
    syncFunction();
    await asyncFunction();
  }

  invokeClassMethods() {
    console.log('Class methods invoked');
  }
}

Now you can use as below:

const myInstance = new MyClass();
myInstance.init();