Skip to content

Instantly share code, notes, and snippets.

@xiongjia
Last active September 14, 2017 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiongjia/7735616 to your computer and use it in GitHub Desktop.
Save xiongjia/7735616 to your computer and use it in GitHub Desktop.
async test - It's a bad .JS. Don't use this scenario in a real server. #devsample
/* async test - It's a bad .JS. Don't use this scenario in a real server.
* 1. The function1 saved the 'callback' and call it when the 'connect' is emitted
* 2. The function3 will be triggered again when the connect event happended.
*/
'use strict';
var events = require("events"),
servEvt = new events.EventEmitter();
function function1(callback) {
console.log('function1');
servEvt.on('connect', function () {
callback();
});
}
function function2(callback) {
console.log('funtion2');
callback();
}
function function3() {
console.log('function3');
}
(function () {
var async = require('async');
/* run funciton1 & function2 in series */
async.series([ function1, function2], function (err, results) {
function3();
});
/* emit the serv 'connect' event every 2 seconds */
setInterval(function () { servEvt.emit('connect'); }, 1000 * 2);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment