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 asyncqueue.js
var async = require('async');
// Send email
var sendEmail = function(email,callback) {
console.log("Sending email to "+email);
callback(null);
}
// create a queue object with concurrency 2
var q = async.queue(sendEmail,2);
View asynccombination.js
var async = require('async');
async.forEach(someData,function(singleData,callback){
async.series();
//OR
async.paralle();
//OR
async.waterfall();
},function(err,data) {
// final callback
View asynceachparallel.js
var async = require('async');
var emails = ["abc@xyz.com","blahblah@aa.com"];
async.each(emails,function(singleEmail,callback) {
async.waterfall([
function(callback) {
// code to send email.
callback(null,Flag);
},
function(emailSentOrNot,callback) {
View asynceachlimit.js
var async = require('async');
var emails = ["abc@xyz.com","blahblah@aa.com"];
async.eachLimit(emails,1000,function(singleEmail,callback) {
// Emailer code
// singleEmail will be one value at a time.
},function(err,data) {
// Once all done, comes here.
});
View asynceach.js
var async = require('async');
var emails = ["abc@xyz.com","blahblah@aa.com"];
async.each(emails,function(singleEmail,callback) {
// Emailer code
// singleEmail will be one value at a time.
},function(err,data) {
// Once all done, comes here.
});
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");
View asyncseries.js
var async = require('async');
async.series([
function(callback) {
// some async task
callback();
},
function(callback) {
// some async task
callback();
View asyncparallel.js
var async = require('async');
async.parallel([
function(callback) {
// Some Async task
callback();
},
function(callback) {
// Some Async task
callback();
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 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.
You can’t perform that action at this time.