Skip to content

Instantly share code, notes, and snippets.

@teramako
Created October 5, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teramako/6842857 to your computer and use it in GitHub Desktop.
Save teramako/6842857 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Serial Experimental proceed</title>
<style>
#out { line-height: 1.5; }
#out > p { margin: 0; }
</style>
</head>
<body>
<h1>Serial Experimental Proceed</h1>
<pre id="out"></pre>
<script>
var out = document.getElementById("out");
function echo (...args) {
var msg = args.join(" ");
var p = document.createElement("p");
p.appendChild(document.createTextNode(msg));
return out.appendChild(p);
}
</script>
<script src="serialExperimental.js"></script>
</body>
</html>
function serialExperimentalProceed (generator) {
var thread = generator();
serialExperimentalProceed.proceed(thread);
}
serialExperimentalProceed.proceed = function proceed (thread) {
var { done, value } = thread.next();
if (done)
return;
if (value instanceof Promise)
value.then(
result => {
proceed(thread);
},
error => {
console.error(String(error));
});
else
proceed(thread);
};
function async (ms, func) {
return new Promise((resolve, reject) => {
setTimeout(()=>{
try {
func();
resolve();
} catch (e) {
reject(e);
}
}, ms);
});
}
serialExperimentalProceed(function * () {
var startTime = Date.now();
echo("Start: " + startTime);
yield async(1000, () => {
echo("Hello");
});
echo("+ " + (Date.now() - startTime));
yield async(1500, () => {
echo("Serial Experimental");
});
echo("+ " + (Date.now() - startTime));
yield async(1500, () => {
echo("Proceed");
});
echo("End: + " + (Date.now() - startTime));
});
echo("start");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment