xFilipe xFilipe - 1 year ago 129
Javascript Question

Why use process.nextTick() before mongoose query?

I have seen lots of Mongo (mongoose ORM) Queries being made inside of process.nextTick() method. I know that nextTick defer the execution to next iteration but since these queries are asynchronous why they use it anyway?


process.nextTick(function() {
User.findOne({ 'local.email' : email }, function(err, user) {

Answer Source

TL;DR: it's not required, or particularly useful.

There are a bunch of Passport tutorials that use this setup: here, here, here.

I have no idea how using process.nextTick() came about, but it seems that these tutorials might be borrowing code from either each other, or a common source (that I haven't been able to find).

Because these tutorials rank pretty high on Google, people find them when they are looking for Passport example code, and copy it. So it appears a lot in SO questions as well, perhaps enforcing the idea that it's somehow required.