Skip to content

Instantly share code, notes, and snippets.

@marek-saji
Last active August 29, 2015 14:01
Show Gist options
  • Save marek-saji/0e14b8bdde718a0fc258 to your computer and use it in GitHub Desktop.
Save marek-saji/0e14b8bdde718a0fc258 to your computer and use it in GitHub Desktop.
async example: (1) get file contents, (2) parse it as a json, (3) add sth, (4) serialize back to string, (5) send a e-mail
function handleError (error) {}
function readFile (PATH, fail, win) {}
function parseJSON (DATA, fail, win) {}
function stringifyJSON (JSON, fail, win) {}
function sendMail (STRING, fail, win) {}
readFile(
PATH,
handleError,
function (DATA) {
parseJSON(
DATA,
handleError,
function (JSON) {
JSON.foo = 42;
stringifyJSON(
JSON,
handleError,
function (STRING) {
sendMail(
STRING,
handleError,
function () {
console.log('at fscking last!');
}
);
}
);
}
);
}
);
function handleError (error) {}
// all of these return Promise
function readFile (PATH) {}
function parseJSON (STRING) {}
function stringifyJSON (JSON) {}
function sendMail (STRING) {}
readFile(path)
.then(parseJSON)
.then(function (JSON) {
JSON.foo = 42;
return stringifyJSON(JSON);
})
.then(sendMail)
.catch(handleError);
function handleError (error) {}
// all of these return Promise
function readFile (PATH) {}
function parseJSON (STRING) {}
function stringifyJSON (JSON) {}
function sendMail (STRING) {}
function* doStuff (PATH)
{
var VALUE;
try
{
VALUE = yield readFile(PATH);
VALUE = yield parseJSON(VALUE);
VALUE.foo = 42;
VALUE = yield stringifyJSON(VALUE);
VALUE = yield sendMail(VALUE);
}
catch (error)
{
handleError(error);
}
}
magic(doStuff);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment