JSFiddle - React, Tailwind, and code Playground
by yakun chen
JavaScript
function makeAjaxCall(url,cb) {
// do some ajax fun
// call `cb(result)` when complete
setTimeout(function() {
cb(url);
}, 1000)
}
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" );
console.log( "The value you asked for: " + result1 );
var result2 = yield request( "http://some.url.2?id=");
console.log( "The value you asked for: " + result2 );
}
var it = main();
it.next(); // get it all started