0

// example

async function getUser() {
  console.log("getUser");
  const user = await fetch("https://api.uomg.com/api/rand.qinghua").then(
    (res) => res.json()
  );
  console.log("getUser已获取到用户数据user", user);
  return user;
}

async function m1() {
  console.log("m1");
  return await getUser();
}

async function m2() {
  console.log("m2");
  return await m1();
}

async function m3() {
  console.log("m3");
  return await m2();
}

async function main() {
  console.log("main");
  const user = await m3();
  console.log("feng", user);
}

main();

In fact ,async function only getUser,but because of it, all subsequent functions become asynchronous.so,How change this?expect your anwser ,Thank u.

In fact ,async function only getUser,but because of it, all subsequent functions become asynchronous.so,How change this?expect your anwser ,Thank u.

  • Replace `async function m1() {` with `function m1() {` and `return await getUser();` with `return getUser();`. Do the same with `m2` and `m3`. – jabaa Aug 06 '23 at 14:30
  • 1
    @jabaa that doesn't make anything any more synchronous. But the point is, you can't make an async function sync, or call it in a sync way. – AKX Aug 06 '23 at 14:36
  • "*because of it, all subsequent functions become asynchronous*" - yes. They are doing something asynchronous, they must return a promise for their result. You cannot change this. Why would you want to? – Bergi Aug 06 '23 at 15:34
  • @AKX The question is how to write the functions without async/await. At least that's how I understand the title. Nobody wants to make anything synchronous. You can write `m1`, `m2` and `m3` as ordinary synchronous functions. – jabaa Aug 06 '23 at 16:00

0 Answers0