cari

Rumah  >  Soal Jawab  >  teks badan

javascript - 怎么用fetch+async模拟jQuery.when

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

阿神阿神2777 hari yang lalu559

membalas semua(2)saya akan balas

  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:31:43

    使用Promise.all

    balas
    0
  • 迷茫

    迷茫2017-04-17 16:31:43

    方法就是 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

    balas
    0
  • Batalbalas