You are right that you cannot go in cycles as you try to make.
Javascript is single threaded. Since as long as the main thread is looped in your while(loop) statement, NOTHING else gets a chance to start. Everything would be fine if your main thread changed the loop variable, but this is not quite what is happening. Instead, you are trying to change the loop variable in the async response, but since your main thread is circular, the async response is NEVER processed, so your loop variable can never be changed and therefore an endless loop is unproductive.
To fix this, you will have to switch to a different loop construct. A common design pattern is to create a local function with the code you want to repeat. Then run the async operation, and if in the async result handler you decide you want to repeat the operation, you simply call the local function again from the inside. Since the result is asynchronous, the stack is unwound, and this is not technically recursion with stack accumulation. It just starts another iteration of the function.
I'm a bit confused by the logic in your code (there are zero comments there to explain this), so I'm not entirely sure I got it right, but here's the general idea:
var generateToken = function(userId) { return new Promise(function(resolve, reject) { User.findOne({userId: userId}, function(err, user) { function find() { var token = Common.randomGenerator(20); User.find({tokens: e}, function(err, result) { if (err) { reject('Error querying the database'); } else { if (result.length === 0) { if (user.tokens === undefined) { user.tokens = []; } user.tokens.push(e); resolve(); } else {
I should notice that you have incomplete error handling in the User.findOne() operation.
FYI, all the logic of a continuous request, until you get result.length === 0 , just seems weird. The logic seems weird and it smells like โpolling a database in a narrow loopโ, which is usually a very bad job. I suspect that there are much more effective ways to solve this problem if we understand the general problem at a higher level.