What does the following code output? Why?

const token = (async () => {
  const token = await new Promise(resolve => {
    setTimeout(() => resolve("abc"), 2000);
  });
  console.log("1", token);
  return token;
})();

console.log("2", token);
May.19,2022

2 and then 1

The token in

2 is a variable assignment, so you don't have to wait for the asynchronous execution to finish
because 1 contains token and token is in the scope of the asynchronous function, so you need to finish the asynchronous code execution before you get the result

.

Open chrome, press F12, type the code into the console and press enter



const token = (async () => {
  const token = await new Promise(resolve => {
    setTimeout(() => resolve("abc"), 2000);
  });
  console.log("1", token);  // 1  abc
  return token;  //   async return Promise
})();

console.log("2", token);  // 2 Promise

in async function, return await is useless. Because async function always contains the return value Promise.resolve, return await doesn't actually do anything unless you add extra time before the overall Promise parses or rejects. This pattern is almost certainly because programmers ignore the return semantics of async function.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3fb17-2b9fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3fb17-2b9fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?