用generator改写ajax

Posted abc1234_abc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用generator改写ajax相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段

[React Testing] Use Generated Data in Tests with tests-data-bot to Improve Test Maintainability(代码片段

html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。

原生的AJAX-改写BML

执行AJAX返回HTML片段中的JavaScript脚本

Sublime Text自定制代码片段(Code Snippets)