Last active
March 22, 2017 15:50
-
-
Save mesaugat/e499f1f3345787be912b70a247b3b56e to your computer and use it in GitHub Desktop.
promise_promise_promise_async_series_callback_then_promise_promise.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const async = require('async'); | |
const axios = require('axios'); | |
const CronJob = require('cron').CronJob; | |
let courseList = [ | |
{courseId: 3}, | |
{courseId: 4}, | |
{courseId: 5}, | |
{courseId: 6} | |
]; | |
const cronFormat = `26 16 27 Jan *`; // need to run this script? change here | |
console.log(`Starting cron at: ${cronFormat}\n`); | |
const job = new CronJob(cronFormat, | |
/** | |
* On cron start. | |
*/ | |
function () { | |
console.log('Here I am.\n'); | |
areYouComedyMe().then(function() { | |
console.log('\nI should be last.'); | |
job.stop(); | |
}); | |
}, | |
/** | |
* On cron complete. | |
*/ | |
function () { | |
console.log('Yeah! We are finished.'); | |
}, | |
/** | |
* Start the job right now. | |
*/ | |
true | |
); | |
/** | |
* Never write shit code like this again. | |
* | |
* @returns {IDontGiveAFuck} | |
*/ | |
function areYouComedyMe() { | |
// return 1 | |
return axios.get('http://google.com/?q=bheja').then(function() { | |
console.log('Level 1'); | |
// return 2 | |
return axios.get('http://google.com/?q=tori_laure').then(function() { | |
console.log('Level 2'); | |
// return 3 | |
return new Promise(function(resolve, reject) { | |
console.log('\nBefore each series:\n') | |
// async.eachSeries | |
async.eachSeries(courseList, function (course, callback) { | |
let _callback = callback; | |
// return 4 | |
return axios.get('http://google.com/?q=' + course.courseId) | |
.then(function () { | |
// return 5 | |
return axios.get('http://google.com/?q=' + course.courseId) | |
.then(function (result) { | |
return async.waterfall([ | |
function(callback) { | |
callback(null, 'one', 'two'); | |
}, | |
function(arg1, arg2, callback) { | |
// arg1 now equals 'one' and arg2 now equals 'two' | |
callback(null, 'three'); | |
}, | |
function(arg1, callback) { | |
// arg1 now equals 'three' | |
callback(null, 'done'); | |
} | |
], function (err, result) { | |
if (err) { | |
console.log('Well well.'); | |
} | |
console.log('Level ' + course.courseId); | |
}); | |
}); // return 5 | |
}).then(function() { | |
// finish single async operation | |
_callback(null); | |
}); // return 4 | |
}, function (err) { | |
if (err) { | |
reject(err); | |
} | |
resolve(null); | |
}); // async.eachSeries | |
}); // return 3 | |
}); // return 2 | |
}); // return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment