Skip to content

Instantly share code, notes, and snippets.

View shaikh-shahid's full-sized avatar
🌎
Building Decentralized Web

Shahid Shaikh shaikh-shahid

🌎
Building Decentralized Web
View GitHub Profile
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);
var async = require('async');
async.forEach(someData,function(singleData,callback){
async.series();
//OR
async.paralle();
//OR
async.waterfall();
},function(err,data) {
// final callback
var async = require('async');
var emails = ["[email protected]","[email protected]"];
async.each(emails,function(singleEmail,callback) {
async.waterfall([
function(callback) {
// code to send email.
callback(null,Flag);
},
function(emailSentOrNot,callback) {
var async = require('async');
var emails = ["[email protected]","[email protected]"];
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.
});
var async = require('async');
var emails = ["[email protected]","[email protected]"];
async.each(emails,function(singleEmail,callback) {
// Emailer code
// singleEmail will be one value at a time.
},function(err,data) {
// Once all done, comes here.
});
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");
var async = require('async');
async.series([
function(callback) {
// some async task
callback();
},
function(callback) {
// some async task
callback();
var async = require('async');
async.parallel([
function(callback) {
// Some Async task
callback();
},
function(callback) {
// Some Async task
callback();
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 {
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.