1. 程式人生 > >fetch發起promise非同步網路請求的個人小記

fetch發起promise非同步網路請求的個人小記

fetch發起非同步網路請求

//fetch請求連結立即返回一個promise物件;promise被通過,返回response物件,再通過response.json()返回的也是一個promise物件,response.json().then()<==>fetchProduct().then();
//具體寫法
exports.fetchProduct = () => {
    return  fetch(`${PREFIX}/product.json`).then(response => response.json());
};

ajax 的實現方法

 exports.xmlProduct
=() =>
{ var xhr = new XMLHttpRequest(); xhr.onload=function(){ insertPhotos(JSON.parse(xhr.responseText)) }; xhr.open("GET",URL); xhr.send(); }