0

I m a beginner in javascript and trying to learn Async Awaits functionality. I want to print functions as per awaits sequence. Please help

  await setTimeout(()=>{
    return "a"
  }, 0)
}

async function b(callback) {
  await setTimeout(()=>{
   return "b"
  }, 1000)
}

async function c(callback) {
  await setTimeout(()=>{
    return "c"
  }, 500)
}

async function d(callback) {
 await setTimeout(()=>{
    return "d"
  }, 4000)
}

//let nm = a(() => {console.log("a")})

async function printVal(){
let w = b();
  w.then(res => {
    console.log(res)
  })
  
let x = await d();
  x.then(res => {
    console.log(res)
  })

let y = await c();
  y.then(res => {
    console.log(res)
  })

let z = await a();
  z.then(res => {
    console.log(res)
  })
  
}

printVal()
Kamal
  • 2,140
  • 8
  • 33
  • 61
  • 1
    The first thing to learn is that `await` only does something useful when you `await` a promise that gets resolved/rejected when your asynchronous operation is complete. `await` works with promises. So, `await setTimeout(...)` does not do anything useful. It does exactly the same thing as just `setTimeout(...)`. – jfriend00 May 01 '21 at 21:01
  • 1
    You can make a timer that works with a promise like this: `function delay(t, v) { return new Promise(resolve => setTimeout(resolve, t, v))}`. Then, you can do `let x = await delay(500, 10)`. – jfriend00 May 01 '21 at 21:03

0 Answers0