Last active
December 31, 2015 04:28
-
-
Save disnet/7934003 to your computer and use it in GitHub Desktop.
sweet.js: async macro
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
let do = macro { | |
rule { $vars (,) ... = $fname ... ($params ...); $rest ...} => { | |
$fname ... ($params ..., function ($vars (,) ...) { $rest ... }) | |
} | |
} | |
var buffer = new Buffer(1024); | |
do err, infile = fs.open("/tmp/in.txt"); | |
do err, outfile = fs.open("/tmp/out.txt"); | |
do err, bytesRead, buffer = fs.read(infile, buffer, 0, 1024, 0); | |
do err, bytesWritten, buffer = fs.write(outfile, buffer, 0, 1024, 0); | |
console.log("We read " + bytesRead + " bytes and wrote " + bytesWritten + " bytes."); |
Named functions are your bestest friends:
fs.open('/tmp/in.txt', next1);
function next1 (err$190, infile$191) {
if (err$190)
throw err$190;
fs.open('/tmp/out.txt', next2);
}
function next2 (err$193, outfile$194) {
if (err$193)
throw err$193;
fs.read(infile$191, buffer$188, 0, 1024, 0, next3);
}
function next3 (err$196, bytesRead$197, buffer$188) {
if (err$196)
throw err$196;
fs.write(outfile$194, buffer$188, 0, 1024, 0, next4);
}
function next4 (err$200, bytesWritten$201, buffer$188) {
if (err$200)
throw err$200;
console.log('We read ' + bytesRead$197 + ' bytes and wrote ' + bytesWritten$201 + ' bytes.');
}
Then you don't need a library, macros, or any other stuff that screws with stack traces.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, welcome to the unhandled contextless exceptions hell?