0

I am trying to resolve a promise after a particular time. This works whenever the resolve function does not have a parameter, but If I want to pass a message and handle it in .then() , the setTimeout part does not work.

For ex in the below code the setTimeout works and promise gets resolved after a particular time:

function wait_time(time) {
  return new Promise((resolve, reject) => {
    //console.log(time)
    setTimeout(resolve, time)
    console.log("Waiting")
    console.log("Still Waiting")
    console.log("Still Waiting")
  })
}

wait_time(800).then((response) => {
  console.log('Hello!' + response)
});

The output of this is as seen below

Waiting

Still Waiting

Still Waiting

Hello!undefined

Now I modified the code as below to pass a parameter via resolve to be handled in .then()

function wait_time(time) {
  return new Promise((resolve('Time'), reject) => {
    //console.log(time)
    setTimeout(resolve, time)
    console.log("Waiting")
    console.log("Still Waiting")
    console.log("Still Waiting")
  })
}

wait_time(800).then((response) => {
  console.log('Hello!' + response)
});

The Output I expect here is

Waiting

Still Waiting

Still Waiting

Hello!Time

But the Output I get is

Hello!Time

It seems the delay does not come into play and promise immediately gets resolved.

Would be great if someone could point out the exact issue and the concept around this?

VLAZ
  • 26,331
  • 9
  • 49
  • 67

0 Answers0