Reputation: 55
I have a nodejs application where i want to make connection with elasticsearch and other database like mongodb and then create server but i don't want to use callback functions . is there any way to hold execution of nodejs code while function is establishing connections with ES cluster.
function loadConfFile(){
}
/* wait for above to complete */
function createESConnection(){
}
/* wait for above to complete */
function createMongoDBConnection(){
}
const express = require('express');
/* and so on */
Upvotes: 0
Views: 183
Reputation: 1213
Wrap into promise with async:
async function createESConnection(){
}
Use await to... well, wait until it ends:
let esConn = await createESConnection()
But there's a caveat: everything using that async function must go async as well (read "go Promise-based")
P.S. Hope you're not restricted to some relic JS version not having that
Upvotes: 1