Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created June 11, 2014 18:20
Show Gist options
  • Save pvdz/15a1765d2127b693e040 to your computer and use it in GitHub Desktop.
Save pvdz/15a1765d2127b693e040 to your computer and use it in GitHub Desktop.
Silly example to add numbers to 5, before processing
var yielding = false;
function this_makeFive(n){
if (n === 3) return this_addOne(n)+1;
else if (n === 1) return this_addThree(n)+1;
else throw 'invalid input, can only hand 1 and 3';
}
function this_addThree(n){
return this_addOne(n) + 2;
}
function this_addOne(n){
throwIfNotStreaming();
return n+1;
}
function throwIfNotStreaming() {
// this is where the parser prematurely reaches the EOF
throw 'Unexpected EOF, cannot yield; not in streaming mode';
}
function get(n, callback) {
var f = this_makeFive(n);
var tmp = f();
if (yielding) {
setTimeout(function again(){
yielding = false;
tmp = f();
if (yielding) {
setTimeout(again, 100);
} else {
callback(tmp);
}
}, 100);
}
}
get(3, function(n){ console.log(3,':',n); });
get(1, function(n){ console.log(1,':',n); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment