rafa
Version:
Rafa.js is a Javascript framework for building concurrent applications.
72 lines (60 loc) • 1.56 kB
Markdown
With promises.
<aside>
```js
function loadStory() {
return getJSON('story.json').then(function(story) {
addHtmlToPage(story.heading);
return story.chapterURLs.map(getJSON)
.reduce(function(chain, chapterPromise) {
return chain.then(function() {
return chapterPromise;
}).then(function(chapter) {
addHtmlToPage(chapter.html);
});
}, Promise.resolve());
}).then(function() {
addTextToPage("All done");
}).catch(function(err) {
addTextToPage("Argh, broken: " + err.message);
}).then(function() {
document.querySelector('.spinner').style.display = 'none';
});
}
```
</aside>
ES7 async functions.
<aside>
```js
async function loadStory() {
try {
let story = await getJSON('story.json');
addHtmlToPage(story.heading);
for (let chapter of story.chapterURLs.map(getJSON)) {
addHtmlToPage((await chapter).html);
}
addTextToPage("All done");
} catch (err) {
addTextToPage("Argh, broken: " + err.message);
}
document.querySelector('.spinner').style.display = 'none';
}
```
</aside>
Rafa
<aside>
```js
function loadStory() {
Rafa.promise(getJSON('story.json'))
.each(story => addHtmlToPage(story.heading))
.split(story => story.chapterURLs)
.burst()
.map(getJson)
.each(chapter => addHtmlToPage(chapter.html))
.error(err => addTextToPage("Argh, broken: " + err.message));
.done(() => {
addTextToPage("All done");
document.querySelector('.spinner').style.display = 'none';
})
}
```
</aside>