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

单个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)

阿神
阿神

闭关修行中......

reply all(2)
伊谢尔伦

Use Promise.all

迷茫

The method is Promise.all(), implemented as follows.

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template