How can I use fetch in while loop

后端 未结 4 1471
名媛妹妹
名媛妹妹 2021-02-09 05:52

My code is something like this:

var trueOrFalse = true;
while(trueOrFalse){
    fetch(\'some/address\').then(){
        if(someCondition){
            trueOrFals         


        
4条回答
  •  鱼传尺愫
    2021-02-09 06:47

    Now with async/await we can use awesome while loops to do cool stuff.

    var getStuff = async () => {
    
        var pages = 0;
    
        while(true) {
    
            var res = await fetch(`public/html/${pages ++}.html`);
    
            if(!res.ok) break; //Were done let's stop this thing
    
            var data = await res.text();
    
            //Do something with data
    
        };
    
        console.log("Look ma! I waited!"); //Wont't run till the while is done
    
    };
    

提交回复
热议问题