[Solved] How to confirm for JavaScript asynchronous calls? [closed]


an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously.

Yes, everything in the async function will run synchronously until await is encountered.

Examples:

async function withoutAwait() {
  console.log('without await')
}

async function withAwait() {
  await 0
  console.log('with await')
}

console.log('start')
withoutAwait()
withAwait()
console.log('end')
let a = 0

async function changeA() {
  a = 20
  await 0
  a = 30
}

console.log('start', a)
changeA().then(() => console.log('after change', a))
console.log('end', a)

P.S.

JavaScript code runs in one thread. No two lines of code run at the same time.

Asnychrouns code just stops executing when it encounters await and goes back to the next fragment when the thread is free.

const results = []

async function f1() {
  for (let i = 0; i < 10; i += 1) {
    await results.push('f1')
    await 0
  }
}

async function f2() {
  for (let i = 0; i < 10; i += 1) {
    await results.push('f2')
  }
}

f1()
f2()
setTimeout(() => {
  console.log(results)
}, 100)

3

solved How to confirm for JavaScript asynchronous calls? [closed]