'Nodejs async/await not working as expected
I have an express API that reads from my fauna database. I have a class setup called Database that handles all my fauna queries, and an async method called getQuotes that essentially gets a fauna document, the general structure is like this:
async getQuotes() {
// note, `query` is an async function
const doc = this.client.query(...).then((res) => {
const data = ...; // to keep this short I'm not gonna show this
console.log("faunadb handler: " + data); // just for debug
return Promise.resolve(data);
})
}
Then when I start the express API, I have it call the getQuotes method and log the data (just for debug).
const quotes = db.getQuotes().then((quotes) => {
console.log("fauna consumer: " + quotes);
})
Now, when I run the app, I get the following output:
starting server on port.... ussual stuff
fauna consumer: undefined
faunadb handler: { ... }
The fauna consumer code runs before we actually get the promise from the fauna query API. I need to use .then because node for some reason doesn't allow me to use await. Does anyone know how to solve this? Thank you!
(using node version v16.14.0, running on Arch Linux)
Solution 1:[1]
try to use await
async getQuotes() {
const res = await this.client.query(...);
const data = ...;
console.log("faunadb handler: " + data); // just for debug
return data;
}
// must be in async function
const quotes = await db.getQuotes();
console.log("fauna consumer: " + quotes);
Solution 2:[2]
The problem is that you need to return data fromgetQuotesPlease just change the code as follows (replace the placeholders):
async getQuotes() {
// note, `query` is an async function
const res = await this.client.query(...)
const data = ...; // to keep this short I'm not gonna show this
console.log("faunadb handler: " + data); // just for debug
return data
}
Solution 3:[3]
getQuotes function should return the result like this.
async getQuotes() {
// note, `query` is an async function
return this.client.query(...).then((res) => {
const data = ...; // to keep this short I'm not gonna show this
console.log("faunadb handler: " + data); // just for debug
return Promise.resolve(data);
})
}
or
async getQuotes() {
// note, `query` is an async function
const doc = await this.client.query(...);
const data = ...; // to keep this short I'm not gonna show this
console.log("faunadb handler: " + data); // just for debug
return Promise.resolve(data);
}
Solution 4:[4]
From my perspective it's better not to mix up await and then. Either use one or another.
I'd recommend to go with await.
async getQuotes() {
// note, `query` is an async function
const res = await this.client.query(...)
const doc = ...; // to keep this short I'm not gonna show this
console.log("faunadb handler: " + doc); // just for debug
return doc;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ahmed El-Tabarani |
| Solution 2 | sina.ce |
| Solution 3 | Fulll Stack Engineer |
| Solution 4 |
