The time in milliseconds to wait before resolving the Promise.
Optionalcb: () => RAn optional callback function to execute before resolving the Promise.
A Promise that resolves after the specified time has elapsed.
// using async/await
async function main() {
console.log('Before timeout.')
await timeout(1000, () => console.log('A second has passed!'))
console.log('After timeout.')
}
main()
// Before timeout.
// A second has passed!
// After timeout.
// using Promise.then()
console.log('Before timeout.')
timeout(
1000,
() => console.log('A second has passed!')
).then(
() => console.log('Then...')
)
console.log('After timeout.')
// Before timeout.
// After timeout.
// A second has passed!
// Then...
Stop execution for a specified amount of time.