Created
March 27, 2014 18:04
-
-
Save shawndumas/9814134 to your computer and use it in GitHub Desktop.
ES6 Spawn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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