While loops using Await Async.

后端 未结 4 1689
旧时难觅i
旧时难觅i 2020-12-29 04:24

This Javascript function seems to use the while loop in an asynchronous way. Is it the correct way to use while loops with asynchronous conditions?

 var Boo         


        
4条回答
  •  梦毁少年i
    2020-12-29 04:54

    Yep, it's fine to do it like this:

        let stopped = false
    
        // infinite loop
        while(!stopped) {
           let res = await fetch('api link') 
           if (res.something) stopped = true // stop when you want
        }
    

提交回复
热议问题