user2957271 user2957271 - 2 months ago 14
Node.js Question

nodejs + sqlite: query execution order

With Sqlite with nodejs you can't take the call execution order equals to the real db execution order of a query.
For example in this pseuso-code you cannot predict if will be inserted A before B or viceversa:

function() {
db.run("INSERT A", function() { });
db.run("INSERT B", function() { });
}


But what about the execution order of the callbacks? If the callback of the INSERT A will be called before the callback of INSERT B you can deduce that A will be inserter before B ?

Answer

When the code execution is started then all the callbacks is passed to event queue in FIFO order and event queue watch the current stack execution and if the stack is empty then instantly pass the first position's callback to stack.

So in your case, if the INSERT A callback is called then it executed first after that INSERT B callback executed.

Here is recommended YouTube link https://www.youtube.com/watch?v=8aGhZQkoFbQ to understand the event loop properly you can take a lot of ideas from here since this Guy explained everything in a suitable way