Last active
August 29, 2015 14:01
-
-
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
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
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!'); | |
} | |
); | |
} | |
); | |
} | |
); | |
} | |
); |
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
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); |
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
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