简体   繁体   English

使用nodejs同步建立elasticsearch连接

[英]Establish elasticsearch connection using nodejs synchronously

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.我有一个 nodejs 应用程序,我想与 elasticsearch 和其他数据库(如 mongodb)建立连接,然后创建服务器,但我不想使用回调函数。 is there any way to hold execution of nodejs code while function is establishing connections with ES cluster.在 function 与 ES 集群建立连接时,有什么方法可以保持 nodejs 代码的执行。

function loadConfFile(){

}
/* wait for above to complete */
function createESConnection(){

}
/* wait for above to complete */
function createMongoDBConnection(){

}

const express = require('express');
/* and so on  */

Wrap into promise with async:使用异步包装到 promise 中:

async function createESConnection(){

}

Use await to... well, wait until it ends:使用 await 来......好吧,等到它结束:

let esConn = await createESConnection()

But there's a caveat: everything using that async function must go async as well (read "go Promise-based")但有一个警告:所有使用异步 function 的东西也必须go 异步(阅读“基于 Promise”)

PS Hope you're not restricted to some relic JS version not having that PS希望你不限于一些没有那个的遗物JS版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM