search

Home  >  Q&A  >  body text

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 days ago558

reply all(2)I'll reply

  • 伊谢尔伦

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

    Use Promise.all

    reply
    0
  • 迷茫

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

    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

    reply
    0
  • Cancelreply