The difference is that your interval
functions returns a promise for when it is finished. You will need to add a return
though so that it works properly:
async function interval() {
await new Promise((res, req) => {
setTimeout(res, 1000)
})
update()
return interval()
//^^^^^^
}
Now when update
throws an exception, the promise is rejected and you can .catch()
the error. Also the interval
recursive calls would stop running, in contrast to the setInterval
which would just continue.
Also async
/await
syntax has the advantage that you can avoid recursion to create loops:
async function interval() {
while (true) {
await new Promise((res, req) => {
setTimeout(res, 1000)
})
update()
}
}