AJAX, callback,promise and generator

AJAX with jQuery javascript

$.ajax({
  url:??
  ,
  type:??
  ,
  data:??
  ,
  success: function(){??} //callback
  ,
  error:function(jqXHR,textStatus,error){??}
})

  think about what AJAX wants from human ,java

  AJAX asks questions :ajax

  tell Me By Which Way You Want To Do Things : —— 'GET or POST' ,json

  tell Me Where The Address Is  —— url ,promise

  tell Me What Data You Want To Get From Or Post On The Server ?——  data ,服务器

  what Do You Want To Do With The Data After Getting Or Posting It Successfully And What If An Eerror Occur? —— callbacksapp

 

AJAX with JS async

window.onload = function(){

  var xhttp = XMLHttpRequest(); 
  // create an AJAX object ...通常要功能检测IE AcitveXObject,其余 XMLHttpRequest   xhttp.open("Get","uri",true);
  // Set AJAX request ————how ,where, async or sync   //若是是‘POST’,则必须设置首部,设置返回内容的编码类型。   //xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;")   xhttp.send();
  //send AJAX request and with (data) if u are posting data to the server.   xhttp.onreadystatechange=function(){
  //readystate状态一有改变的话,就执行这个匿名函数,因此会执行4次
    if( xhttp.readystate== 4 && xhttp.status==200){
   /*通常是检测到readystate为4,      且status 返回200时,(HTTP状态码 200表明处理成功OK,404找不到文件等等),      再执行真正的callback处理返回的数据;*/         callback(xhttp.response)
       //and if it's json data ,u would have to use JSON.parse       }   } }

若是须要多个AJAX 请求的话,为了不写出 callback hell 能够用promise 对象,和 generator.函数

1,promise 通常是用new 建立Promise对象,post

在promise对象上用then(callback)来处理数据,// then在这里,大概就是而后的意思。

若是后面还有AJAX request的话,则能够在callback 内部 return 新的 new Promise 对象,而后对执行后返回的promise对象使用.then(callback)方法。

Promise机制 是链式的。

2,generator 生成器 ——— function*(yield  ??){} 

var myGenerator = function*(){

  var data1 = yield $.get(url1);
  //do sth with data1
  var data2 = yield $.get(url2);

  var data3 = yield $.get(url3)

}

wrapGener (myGenerator) // 把generator 传递给 wrapGener

function wrapGener ( generator ){

  var gener = generator(); //生成生成器 
  return handleGenerator(gener.next()) //调用next方法,传递生成的对象 Object{value:"??",done:false} 给handleGenerator。
    function handleGenerator(yieldedObj){ 
      if( !yieldedObj.done){//若是生成器没有执行完毕

        yieldedObj.value.then(//执行callback

          function(data){ // callback 的参数 Object.value 
          return handleGenerator(gener.next(data))//递归 返回此次的object.value 给gener,赋值给包含yield的变量。而且再次调用gener的next生成新的对象。
         })       
        }    
     }
}

  var gener = generator();

//call 一个生成器并不会马上执行,而是准备执行,有点像new func() 的建立对象,可是并无new关键字 。

只有call了gener .next()方法,生成器才会开始执行,可是,执行到有 yield 关键字 的地方会暂停,并且,会返回 yield 后面的值。

// yield 差很少就是 return for now .

若是在执行过程当中,遇到 N个 yield AJAX request ,则须要调用N个.next()方法。

通常是在检测 yield 返回的对象的done属性不为真 ,也就是说生成器尚未执行完毕。Object.done == false 的状况下调用下一个next方法。

yield 返回给调用者的是一个有着2个属性的对象,Object{value:"??",done:false}

而 Object.value 里面的值就是 yield 后面的 AJAX request 了,也就是服务器返回的内容。

对 Object.value 调用.then(callback)方法,就可使用内容了。

能够在callback function内部用递归的方式,再次调用 gener .next(value) ,这个时候会返回object.value里面的值给gener,而后generator继续执行。

相关文章
相关标签/搜索