Last active
September 14, 2017 07:44
-
-
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
This file contains 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
/* 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