An async
function always returns a promise. The resolved value of that promise is whatever value the code in your function returns. So, to get the value out of that promise, you use either await
or .then()
;
getFromDB().then(val => {
// got value here
console.log(val);
}).catch(e => {
// error
console.log(e);
});
There is no free lunch in Javascript. A value obtained asynchronously can only be returned from a function asynchronously (via callback or promise or other similar async mechanism).
Or, if the caller itself was an async function, then you could use await
:
async function someOtherFunc() {
try {
let val = await getFromDb();
console.log(val);
} catch(e) {
// error
console.log(e);
}
}