Having code that resembles the snippet below, I am uncertain about the order of execution it follows. Currently, it seems to run in a non-blocking manner:
func() -> self.db.createEntry() -> res.on() -> callback -> self.submit()
However, there have been instances where it operated as:
func() -> self.db.createEntry() -> callback -> res.on() -> self.submit()
Since res.on('data')
acts as a socket event listener handled on a separate server and is beyond my control, I worry that the callback might get invoked amidst res.on()
. Is it possible for the callback to interrupt the ongoing res.on()
process if called midway through?
const func = function(){
self.db.createEntry(self.arg, self.arg1, function(response){
if(response){
self.arg = response;
self.state.wait = false;
if(self.state.wait){
self.submit();
}
}
});
res.on('data', function(data) {
data = parse(data);
if(!data.contentId){
self.state.wait = true;
self.state.data = data;
}
else {
self.submit(data);
}
});
}
func();
db.prototype.createEntry = function(arg, arg1, callback) {
self.connect();
self.connection.query('INSERT INTO table_name SET ?', arg, function(err, results, fields) {
if(err) {
self.disconnect();
callback();
}
else {
self.disconnect();
callback(results);
}
});
}