Skip to content

Instantly share code, notes, and snippets.

@shawndumas
Created March 27, 2014 18:04
Show Gist options
  • Save shawndumas/9814134 to your computer and use it in GitHub Desktop.
Save shawndumas/9814134 to your computer and use it in GitHub Desktop.
ES6 Spawn
var spawn = function (generatorFn) {
var continuer = function (verb, arg) {
var r;
try {
r = generator[verb](arg);
} catch (error) {
return Promise.reject(error);
}
if (r.done) {
return r.value;
} else {
return Promise
.resolve(r.value)
.then(fulfilled, rejected);
}
},
generator = generatorFn(),
fulfilled = continuer.bind(continuer, 'next'),
rejected = continuer.bind(continuer, 'throw');
return fulfilled();
};
var loadStory = function () {
return spawn(function *() {
try {
let story = yield getJSON('story.json');
addHtmlToPage(story.heading);
for (let chapter of story.chapterURLs.map(getJSON)) {
addHtmlToPage((yield chapter).html));
}
addTextToPage("All done");
} catch (error) {
addTextToPage("Argh, broken: " + error.message);
}
document.querySelector('.spinner').style.display = 'none';
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment