How do I wait for one function to finish executing asynchronously before executing another function?

  1. problem description

there are two functions F1 and f2, in which there is an asynchronous operation in F1. After the execution of F1, the execution of F1 is followed by the execution of f2. Because there is an asynchronous operation inside F1, it will first execute F1, and then execute the async in F1. How to wait for F1 to execute asynchronously before executing f2.

Note: the execution mode is F1 (); f2 (); do not want to include promise outside F1 and f2.

  1. Code
const f1 = async () => {
    const p = new Promise((resolv, reject)  => {
    setTimeout(() => {
      console.log("")
      resolv("resolv")
    }, 2000);
  })
    let res = await p;
  console.log("res", res);
}

const f2 = () => {
    console.log("f2")
}

f1();
f2();

// 
// f2
// 
// res reslve
  1. the desired results are printed out as follows

I am asynchronous
res reslve
I am f2

f2 requires that the asynchronous function in F1 be executed after it is finished-sharp-sharp-sharp problem description

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Jun.09,2022

await F1 ()
f2 ()
? How understandable.


run f2 directly in the then of F1

Menu