Skip to content

Instantly share code, notes, and snippets.

@bentomas
Created February 16, 2010 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bentomas/305252 to your computer and use it in GitHub Desktop.
Save bentomas/305252 to your computer and use it in GitHub Desktop.
var posix = require("posix");
var sys = require("sys");
var continuables = require('continuables');
function writeFile(filename, value, mode) {
if (typeof value !== "string") {
throw new Error('Value must be a string');
}
if (typeof mode === "undefined") {
mode = 0600;
}
/* Create a proper temp filename */
var date = new Date();
var tmp = process.platform + "." + process.pid + "." +
Math.round(Math.random()*1e6) + "." + (+date) + "." +
date.getMilliseconds();
var filenameTmp = filename + "." + tmp + ".tmp";
// we'll return this continuable when we are done
var continuable = continuables.create();
// add some callbacks
continuable
(function(fd) {
if( !(fd instanceof Error) ) {
// we have to go to these great lengths because the return value from the
// posix.write promise doesn't give us the fd
var cont2 = continuables.create();
var p2 = posix.write(fd, value);
p2.addCallback(function(written) {
// fulfill our continuable with the fd
cont2.fulfill(fd);
});
p2.addErrback(function(err) {
cont2.fulfill(err);
});
return cont2;
}
})
(function(fd) {
if( !(fd instanceof Error) ) {
// just return the promise
return posix.close(fd);
}
})
(function(result) {
if( !(result instanceof Error) ) {
return posix.rename(filenameTmp, filename);
}
})
(function(result) {
if( result instanceof Error ) {
// remove the temp file, and don't listen for its errors
posix.unlink(filenameTmp).addErrback(function(){});
}
})
// at this point we should know if we errored or not
// here we actually start the callback chain from the continuable we created
var p = posix.open(filenameTmp, process.O_CREAT|process.O_WRONLY, mode);
p.addCallback(function(fd) {
continuable.fulfill(fd);
});
p.addErrback(function(err) {
continuable.fulfill(err);
});
return continuable;
};
writeFile('hello.txt', 'Hello')
(function(val) {
sys.p(val);
// make it so any errors don't get thrown
return true;
});
/*
Hypothetical connection pool
1. checks out a database connection from a pool
2. sends a query
3. reads the results
4. checks in the connection again
*/
Pool.checkout('database name') // 1.
(function(db) {
if(!(db instanceof Error)) {
database = db;
return db.query('SELECT * FROM...'); // 2.
}
})
(function(results) {
if(!(results instanceof Error)) {
results.forEach(function(row) { // 3.
// use the row here
});
}
})
(function(results, success) {
// regardless of what happened checkin the database
Pool.checkin('database name'); // 4.
});
// at this point if there was an error, that will be passed to the next
// callback, and if not the next callback would get the results from the query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment