//helper function for DOM manipulation
var updateDOM = function(res,res2,res3){
$('#res').html(JSON.stringify(res));
$('#res2').html(JSON.stringify(res2));
$('#res3').html(JSON.stringify(res3));
};
//Defines a generator to call a JSON webservice.
//info 1: for each "yield" a separate generator call is needed
//this is why generators are called "pausable functions"
//info 2: because we yield jQuery ajax calls the return values are
//promises and therefore we'll later use "then" to resolve values
//info 3: when dealing with generators one SHOULD use a proper library
//like Bluebird: https://github.com/petkaantonov/bluebird
//or Q: https://github.com/kriskowal/q/
//This code is for demonstration purposes only!
var initJsonGenerator = function*() {
var res = yield $.get("http://md5.jsontest.com/?text=callOne");
var res2 = yield $.get("http://md5.jsontest.com/?text=callTwo");
var res3 = yield $.get("http://md5.jsontest.com/?text=callThree");
updateDOM(res,res2,res3);
};
//This function takes an initialized generator and calls its next() method.
//****************************************************************************
//Each time a next() method gets called a "yield" from within the generator
//will be executed (yes, yes, it behaves like an Iterator but with generators
//one doesn't have to maintain internal state/position etc.).
//****************************************************************************
//The returned values will contain the indicator "done"
//which tells us the current state of generator execution. As long as
//"done" is not set to "true" there's still some work to do (that is, some "yields" to
//get executed). And because the return values in this example contain promises
//we'll call their "then" methods to receive the returned MD5-hashes.
var useGenerator = function(gen) {
//some "random" number to mark different calls
var id = Math.floor(Math.random()*99999);
$('#output').append('<p>Call ID: ' + id...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.