M
M
MrAlkatras2018-04-21 15:39:26
MongoDB
MrAlkatras, 2018-04-21 15:39:26

Why does mongo connect to the db only after all query operations are done?

Hi everyone, please advise.
There is a handler function
res.end(handler);

function handler() {
            function connect() {
            var data = "no data";
             mongo.connect(...) {...
             подключение к БД и получение результата
             console.log("connected")
            }
         }
     вывод результата
     console.log("result")
}

Why does the function work in such a way that it is displayed immediately in the console and after it
"result"
?
"connected"
connectdatahandler"connected"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-04-21
@MrAlkatras

Because the mongo.connect() operation is asynchronous. And the event loop doesn't stop at it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question