When I call an async function, my expectation is to receive a JSON payload that will allow me to extract a specific value from the object. However, all I get in return from the function is [object Object]
This async function was initially called within a normal function, so I attempted changing the parent function to async as well. This adjustment proved to be beneficial as it originally resulted in only receiving a value of [object Promise]
The initial attempt looked like this:
const sendRequestToApprover = (userId, announcement, username) => {
const { title, details, channel } = announcement;
const channelName = channels.getChannelName(channel);
console.log('channelName' + channelName);
and the getChannelName
function appears as follows:
async function getChannelName(id) {
try {
return await api.get('/channels.info', {
params: {
token: botToken,
channel: id,
}
});
} catch (err) {
console.log(err);
}
}
To resolve the issue of getting [object Promise]
, I made the following modification to sendRequestToApprover
:
async function sendRequestToApprover(userId, announcement, username) {
const { title, details, channel } = announcement;
const channelName = await channels.getChannelName(channel);
console.log('channelName' + channelName);
It's important to note that the function is now async and I included await
in the function call.
I'm aware that the expected payload will resemble the following structure:
{
"ok": true,
"channel": {
"id": "CJSFDR83T",
"name": "general",
...
}
Despite knowing the expected format of the payload, I'm unable to access the name property in this particular case. I've successfully achieved this in other functions, but for some reason, this one has left me scratching my head. The variable channelName
continues to display [object Object]
.