I'm trying to return an async function but I either get promise: < { PENDING } > or [object Promise] instead of [object Object]
I've tried returning the value using Promise.resolve(value), Promise.resolve().then(return value), return new Promise((resolve, reject) => {resolve(value)}
from top-level to bottom my code looks like:
//Top-Level
const getNext = require('getNext');
const next = getNext({
page,
value,
name,
id,
});
//getNext
const controllerMap = {
intro: introController
};
async function getNext({ page, value, name, id}) {
const controller = controllerMap[name];
return await controller({
page,
value,
name,
id
});
}
// Controller
async function introController({ page, value, id }) {
switch(page)
case 10:
// Do something async ie:
await db.query
default: return intro[page]
};
If I take off async and await from the functions and extract my low-level db.query from the controller case into it's own async function I just get promise: < { PENDING } > so I'm thinking it's because the top level functions aren't waiting for it to resolve. However, when I make those functions async they return promises for my static data. I'm having trouble wrapping my head around these nested promises/async functions.
awaitthe call togetNext.then()Promises. You need to useawaitalongside theasyncor use.then().awaitinside anasyncfunction, and the call togetNextis not inside a function.