How to slow down javascript execution before receiving a callback?

In order to obtain data for the page, the server sends a query to the database. The result comes in the form of a callback variable that stores the JSON object.
JSON object should be used when rendering the page. But when I run the page render faster than comes the callback. How can I pause the function to obtain a Callback?

Javascript code:
var Datastore, JPug, Pug, app, async, db, engine, findPage, koa, page;

async = require('async');
Datastore = require('nedb');

db = {};
db.pages = new Datastore({
 filename: 'DB/pages/index.db',
 autoload: true
});

koa = require('koa');
app = koa();

Pug = require('koa-pug');
engine = new Pug({
 viewPath: './views',
 debug: false,
 pretty: true,
 compileDebug: false,
 locals: {},
 basedir: __dirname + '/public',
 app: app
});

findPage = function(req, json) {
db.pages.find({
 url: req
 }, function(err, docs) {
console.log(docs);
 json = docs; // Desired data for the page
});
};

app.use(function *() {
 'use strict';
 var json req;
 req = this.request.url;
console.log(req);
async.series([
 findPage(req, json) 
 page = this.render('index', {page: json}, true) // render page
 ], function() {
 return page;
});
});

app.listen(3000);

When the code runs, comes a rendering error, because the variable json is not defined. And after issued received callback:

172a947e67f34b7f902f80b308a2fb18.png
I hope for your ideas and comments. Thanks in advance.
July 8th 19 at 12:16
3 answers
July 8th 19 at 12:18
Solution
From VEB to organize asynchronous data retrieval.
Ie have made a brand new page, and js any case, the server for data and uploads them as they are, but then You will need a caching mechanism at the moment when the client is not redrawn, it will render the data itself and the server is already out of the database and send the result to nowhere.

For example, a page opens a socket with the id 1, id generates the server before the query to the database, so when the database responds, it can put to the cache data is requested by id and the socket on the page will be able to take this data from the cache knowing id. Something like that.

Can even just for a moment the fact. retrieve data from the database to render the page.
July 8th 19 at 12:20
No way.
The request is performed asynchronously. You need to run the render after receiving a response, or rewrite things so that these data were optional at the beginning of the render.
July 8th 19 at 12:22
findPage = function(req, json, cb) {
db.pages.find({
 url: req
 }, function(err, docs) {
 if (err) cd(err);
console.log(docs);
 json = docs; // Desired data for the page
 cb(null, json)
});
};

app.use(function *() {
 'use strict';
 var json req;
 req = this.request.url;
console.log(req);
 findPage(req, json, function(err, data) {
 if (err) throw err;
 page = this.render('index', {page: data}, true) // render page
});
});

And do you use async, it would have used them for serial execution of tasks.
https://github.com/caolan/async#seriestasks-callback

Find more questions by tags JavaScriptNode.js