码迷,mamicode.com
首页 > Web开发 > 详细

用generator改写ajax

时间:2019-02-02 18:53:22      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:tor   func   turned   ons   from   resume   ted   pre   style   

function request(url) {
    // this is where we‘re hiding the asynchronicity,
    // away from the main code of our generator
    // `it.next(..)` is the generator‘s iterator-resume
    // call
    makeAjaxCall( url, function(response){
        it.next( response );
    } );
    // Note: nothing returned here!
}

function *main() {
    var result1 = yield request( "http://some.url.1" );
    var data = JSON.parse( result1 );

    var result2 = yield request( "http://some.url.2?id=" + data.id );
    var resp = JSON.parse( result2 );
    console.log( "The value you asked for: " + resp.value );
}

var it = main();
it.next(); // get it all started

 

用generator改写ajax

标签:tor   func   turned   ons   from   resume   ted   pre   style   

原文地址:https://www.cnblogs.com/panyujun/p/10348855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!