简体   繁体   中英

Why my resolve doesn't wait for the specified time?

I am trying to call the resolve after 10 seconds but I guess it is called directly.

let timer = (time) => new Promise((resolve)=> { setTimeout( resolve("success"),time)});

timer(10000).then( (result) => {
    alert(result)
})

You're passing the return result of resolve() to setTimeout. You are not passing resolve() to setTimeout.

Remember basic programming in any programmig language (php, javascript, C, java etc.):

foo(bar())
// is the same as
temp = bar()
foo(temp)

So the correct way to pass resolve() to setTimeout is:

setTimeout(() => resolve("success"),time)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM