javascript - 怎么用fetch+async模拟jQuery.when
阿神
阿神 2017-04-17 16:29:43
0
2
555

单个fetch+async

(async() => {
  try {
    var response = await fetch(url);
    var data = await response.json();
    console.log(data);
  } catch (e) {
    console.log("Booo")
  }
})();

如何像$.when一样发起多个请求

  $.when(...reqArr).done(function (...data) {

  }  

类似

requestByFetch(urls)

阿神
阿神

闭关修行中......

Antworte allen(2)
伊谢尔伦

使用Promise.all

迷茫

方法就是 Promise.all() , 实现如下.

let all = async (urls) => {
    let get = async(url) => {
        let res = await fetch(url);
        ...
        return res;
    }
    let promises = urls.map(async (url) => await get(url));
    let data = await Promise.all(promises);
    return data;
}

soonfy

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage