1

I have an asynchronous function that is called from a synchronous function as shown below.

public syncFunction(data: Data): Value[] {
    const results: Value[] = [];
    // read data object and do some stuff
    results.push(data.value)
    // Call async function
    const val = this.asyncFunction()
    results.push(val);
    return results;
  }
public async asyncFunction(): Value {
    let result = await this.anotherAsyncFunction();
    // Do some stuff
    return result;
  }

By doing this, the type of the variable named val it is, of course, a Promise. What I would like to do is resolve the promise, push the value in the resultsarray and than return the array. I tried to use this.asyncFunction.then() but the return statements is executed first.
Is it possible to do something like this?
Within the project all functions are synchronous and I do not have the possibility to modify it

AmD
  • 399
  • 2
  • 12
  • What you need to check is this - https://learn.javascript.ru/async-await – Sh. Pavel Jul 30 '21 at 11:05
  • 2
    *"Is it possible to do something like this?"* I'm afraid not, no. You can't return the result of an asynchronous operation from a synchronous function, by their very nature. The asynchronous result isn't available when the synchronous function exits. – T.J. Crowder Jul 30 '21 at 11:06
  • @Sh.Pavel - It's clear that the OP knows how to use `async`/`await`. – T.J. Crowder Jul 30 '21 at 11:06

0 Answers0