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
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 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();
View asyncseries.js
var async = require('async');
async.series([
function(callback) {
// some async task
callback();
},
function(callback) {
// some async task
callback();
View asyncwaterfall.js
var async = require('async');
async.waterfall([
function(callback) {
// some code to execute
// in case to go to next function provide callback like this.
callback(null,valueForNextFunction);
// Got some error ? Don't wanna go further.
// Provide true in callback and execution will stop.
//callback(true,"Some error");
You can’t perform that action at this time.