Welcome
» NERWous C
» Examples
CommonJS Promises
The goal of the CommonJS group is to build a better JavaScript ecosystem. One of its proposal is Promises/A: "a promise represents the eventual value returned from the single completion of an operation".
The following example to display the web contents of the first web link in a tweet, is taken from an article that exalts the use of Promises/A:
NERWous C Sample
The verbose first version shows all the tasks:
The generic keyword
Removing the NERWous C symbols from the compact version above results in the synchronous C version:
Previous Next Top
CommonJS Promises
The goal of the CommonJS group is to build a better JavaScript ecosystem. One of its proposal is Promises/A: "a promise represents the eventual value returned from the single completion of an operation".
The following example to display the web contents of the first web link in a tweet, is taken from an article that exalts the use of Promises/A:
getTweetsFor("domenic") // promise-returning function
.then(function (tweets) {
var shortUrls = parseTweetsForUrls(tweets);
var mostRecentShortUrl = shortUrls[0];
return expandUrlUsingTwitterApi(mostRecentShortUrl); // promise-returning function
})
.then(httpGet) // promise-returning function
.then(
function (responseBody) {
console.log("Most recent link text:", responseBody);
},
function (error) {
console.error("Error with the twitterverse:", error);
}
);
The promise of Promises/A is that promises can be chained, and exceptions can bubble up to someone who can handle that failure.
NERWous C Sample
The verbose first version shows all the tasks:
/* VERSION 1 - Asynchronous */
try {
<pel> p_tweets = <!> getTweetsFor("domenic"); // parallel execution
char* tweets = <?>p_tweets; // the mel wait unblocks the thread for other tasks
char* shortUrls = parseTweetsForUrls(tweets); // serial execution
char* mostRecentShortUrl = shortUrls[0]; // serial execution
<pel> p_twitter = <!> expandUrlUsingTwitterApi(mostRecentShortUrl); // parallel
char* url = <?>p_twitter; // mel wait unblock the thread for other tasks
<pel> p_http = <!> httpGet(url); // parallel execution
char* responseBody = <?>p_http; // mel wait unblock the thread for other tasks
printf ("Most recent link text: %s", responseBody);
}
catch (p_tweets<...>) {
printf ("Error [%s] on [getTweetsFor] due to [%s]",
p_tweets<exception>, p_tweets<why>);
}
catch (p_twitter<...>) {
printf ("Error [%s] on [expandUrlUsingTwitterApi] due to [%s]",
p_twitter<exception>, p_twitter<why>);
}
catch (p_http<...>) {
printf ("Error [%s] on [httpGet] due to [%s]",
http<exception>, http<why>);
}
The 2nd version is more compact, and shows that the tasks can be chained together, and that errors can bubble to the top exception handler:
/* VERSION 2 - Asynchronous */
try {
char* shortUrls = parseTweetsForUrls(<!><?> getTweetsFor("domenic"));
char* mostRecentShortUrl = shortUrls[0];
char* responseBody = <?><!> httpGet(
<?><!> expandUrlUsingTwitterApi(mostRecentShortUrl)
);
printf ("Most recent link text: %s", responseBody);
}
catch (pel<...>) {
printf ("Error [%s] on [%s] due to [%s]",
pel<exception>, pel<name>, pel<why>);
}
The double header <?><!>
means forking the task to run in parallel (<!>
) and then waiting for its result (<?>
).
The generic keyword
pel
in the catch
statement represents a task that fails. The name of the task is found via the property pel<name>
.
Removing the NERWous C symbols from the compact version above results in the synchronous C version:
/* VERSION 3 - Synchronous */
try {
char* shortUrls = parseTweetsForUrls(getTweetsFor("domenic")); /* blocking */
char* mostRecentShortUrl = shortUrls[0];
char* responseBody = httpGet(
expandUrlUsingTwitterApi(mostRecentShortUrl)
); // blocking x 2
printf ("Most recent link text: %s", responseBody);
} catch (error) {
printf("Error with the twitterverse: %s ", error);
}
In other words, it is sometimes very easy to transform a serial synchronous version into a parallel asynchronous version using NERWous C. Just pepper it juicily with pel (<!>
) and mel (<?>
) constructs.
Previous Next Top
No comments:
Post a Comment