Open radiolondra opened 1 year ago
Hey, I do not think this is an issue. Your then()
is likely executing after DoSomethingInDB()
so server
is not even assigned to yet. To avoid this, in an async function, you need to await db.open()
.
Try this for example:
var server;
async function OpenDB() {
await db.open("somedb").then(function (s) {
server = s;
});
}
function DoSomethingInDB() {
console.log("Server var:", server);
server.mytable.count().then(function (ct) {
});
}
async function Exec(){
await OpenDB();
DoSomethingInDB();
}
Exec();
Anyway, this is how promises work and there is nothing wrong with the library itself in the example you provided.
I'm using a function to open the database and try to use the assigned
server
variable in other functions:My database is successfully created in
OpenDB()
. Unfortunately, it seems that the globalserver
var becomes undefined outside theOpenDB
function. From documentation:But I didn't close it. Bah? For sure I'm doing something wrong. Can you help me? Thanks.