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 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 let = macro { | |
rule { async $vars ... = $fname ... ($params ...); $rest ...} => { | |
$fname ... ($params ..., function (err, $vars ...) { | |
if (err) throw err; | |
$rest ... | |
}) | |
} | |
} | |
var buffer = new Buffer(1024); | |
let async infile = fs.open("/tmp/in.txt"); | |
let async outfile = fs.open("/tmp/out.txt"); | |
let async bytesRead, buffer = fs.read(infile, buffer, 0, 1024, 0); | |
let async bytesWritten, buffer = fs.write(outfile, buffer, 0, 1024, 0); | |
console.log("We read " + bytesRead + " bytes and wrote " + bytesWritten + " bytes."); | |
// compiles to: | |
var buffer$188 = new Buffer(1024); | |
fs.open('/tmp/in.txt', function (err$190, infile$191) { | |
if (err$190) | |
throw err$190; | |
fs.open('/tmp/out.txt', function (err$193, outfile$194) { | |
if (err$193) | |
throw err$193; | |
fs.read(infile$191, buffer$188, 0, 1024, 0, function (err$196, bytesRead$197, buffer$188) { | |
if (err$196) | |
throw err$196; | |
fs.write(outfile$194, buffer$188, 0, 1024, 0, function (err$200, bytesWritten$201, buffer$188) { | |
if (err$200) | |
throw err$200; | |
console.log('We read ' + bytesRead$197 + ' bytes and wrote ' + bytesWritten$201 + ' bytes.'); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Named functions are your bestest friends:
Then you don't need a library, macros, or any other stuff that screws with stack traces.