欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js使用generator函數(shù)同步執(zhí)行ajax任務(wù)

 更新時(shí)間:2017年09月05日 11:42:26   作者:在這個(gè)肆意的青春歲月  
這篇文章主要為大家詳細(xì)介紹了js使用generator函數(shù)同步執(zhí)行ajax任務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js使用generator函數(shù)同步執(zhí)行ajax任務(wù)的具體代碼,供大家參考,具體內(nèi)容如下

function request(url, callback) {
  fetch(url, {mode: 'cors', credentials: 'include', headers: new Headers({ 'X-Requested-With': 'XMLHttpRequest' })})
  .then(response => response.text())
  .then(text => {
    console.log(url);
    console.log(text);
    callback(text);
  })
  .catch((e) => console.log(e));
}

var iterator = null;
function getData(src){
  request(src, function(response){
    iterator.next(JSON.parse(response));
  })
}

function getTpl(src){
  request(src, function(response){
    iterator.next(response);
  });
}

// 同步任務(wù)
function render(data, tpl){
  for(var i in data) {
    tpl = tpl.replace("${"+i+"}", data[i]);
  }
  return tpl;
}

// 主邏輯
var getArticles = function* (src){
  console.log('begin')
  var data = yield getData(src)
  var tpl = yield getTpl(data.tpl)
  var res = render(data, tpl)
  console.log(res)
}

iterator = getArticles('data.json')
// 開(kāi)始執(zhí)行
iterator.next()
// 異步任務(wù)模型

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論