Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mlconnor
Created October 18, 2016 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlconnor/346a2a4ebc21b7d96e9fa3ca0e4a7c78 to your computer and use it in GitHub Desktop.
Save mlconnor/346a2a4ebc21b7d96e9fa3ca0e4a7c78 to your computer and use it in GitHub Desktop.
Finally, a way to do node.js synchronously
'use strict';
var fs = require('fs');
var co = require('co');
var Promise = require('bluebird');
var readFile = Promise.promisify(require("fs").readFile);
function* doAsyncStuff() {
var file1 = yield readFile('testx.txt',{encoding:'utf8'});
return file1;
}
co( function *() {
var file1 = yield doAsyncStuff();
console.log('done ' + file1);
}).catch( function(e) {
console.log(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment