Skip to content

Instantly share code, notes, and snippets.

Shahid Shaikh codeforgeek

Block or report user

Report or block codeforgeek

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@codeforgeek
codeforgeek / movieName
Created Jun 24, 2015
Movie name in JSON format
View movieName
[
{
"name":"The 40-Year-Old Virgin"
},
{
"name":"51 Birch Street"
},
{
"name":"The Adventures of Sharkboy and Lavagirl in 3-D"
},
View sync.js
var fs = require("fs");
var fileContent = fs.readFileSync('sync.js','utf8');
console.log(fileContent);
console.log("something else");
//Output
//Same code content
//something else
View async.js
var fs = require("fs");
fs.readFile('async.js','utf8',function(err,data){
if(!err) {
console.log(data);
}
});
console.log("something else");
View callbackhell.js
var fs = require("fs");
var db = require('somedbfile.js');
var sendEmail = require('someEmail.js');
fs.readFile('async.js','utf8',function(err,data){
if(!err) {
console.log(data);
}
db.executeQuery('SELECT * FROM test',function(err,rows) {
if(!err) {
console.log("Error",err);
View simplecallback.js
var fs = require("fs");
fs.readFile('async.js','utf8',function(err,data){
if(!err) {
console.log(data);
}
});
View modularsolution.js
var fs = require("fs");
fs.readFile('async.js','utf8',fileContent);
function fileContent(err,data) {
if(!err) {
console.log(data);
}
}
View generators.js
"use strict";
var fs = require('fs');
var co = require('co');
co(function* () {
var file1 = yield readFile('demo.js');
console.log(file1);
console.log("I am after file read even though its Async");
});
View promise.js
var Promise = require('bluebird');
// Converts all function of 'fs' into promises.
var fs = Promise.promisifyAll(require('fs'));
fs.readFileAsync('file.js','utf8')
// 'then' when result comes.
.then(function(data) {
console.log(data);
})
//'catch' when error comes.
View eventcallback.js
var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
var fs = require('fs');
// Event to read file - generic function.
emitter.on('start_read',function(file_name) {
console.log("Started Reading file....\n\n");
fs.readFile(file_name, 'utf8', function (err,data) {
if (err) {
console.log("Error happens.");
} else {
View asyncparallel.js
var async = require('async');
async.parallel([
function(callback) {
// Some Async task
callback();
},
function(callback) {
// Some Async task
callback();
You can’t perform that action at this time.