As per the title, I want to solve this error message:"Parsing error: Unexpected reserved word 'await'. (10:22)eslint"
This is my current, working code snippet:
import { WebClient } from '@slack/web-api';
import { prSummaryBotEnvVariables } from '../pr-summary-bot-env/env-exported-variables.js';
import { getUsersFirstAndLastName, removeUndefinedValuesFromArray } from '../utils/utils.js';
const botToken = prSummaryBotEnvVariables();
const client = new WebClient(botToken.botUserOauthToken);
let slackUserEmails;
const client = new WebClient(botToken.botUserOauthToken);
let slackUserEmails;
try {
const { members } = await client.users.list();
slackUserEmails = members.map(member => member.profile.email);
} catch (error) {
console.error(error);
}
const slackUserNames = removeUndefinedValuesFromArray(getUsersFirstAndLastName(slackUserEmails));
export { slackUserNames };
I'm just using Slack API to get all our members' usernames & do some manipulation with it before exporting the array
. I tried this solution with two inner async
keywords, but it didn't work. How can I refactor this?
Update:
I tried this solution, but I just get an empty array
:
import { WebClient } from '@slack/web-api';
import { prSummaryBotEnvVariables } from '../pr-summary-bot-env/env-exported-variables.js';
import { getUsersFirstAndLastName, removeUndefinedValuesFromArray } from '../utils/utils.js';
const botToken = prSummaryBotEnvVariables();
const client = new WebClient(botToken.botUserOauthToken);
let slackUserEmails;
const slackUserNames = [];
(async () => {
try {
const { members } = await client.users.list();
slackUserEmails = members.map((member) => member.profile.email);
} catch (error) {
console.error(error);
}
slackUserNames.push(removeUndefinedValuesFromArray(getUsersFirstAndLastName(slackUserEmails)));
})();
console.log(slackUserNames); // empty array
export { slackUserNames };
CodePudding user response:
As mentioned in the comments, you cannot use the await
keyword outside of async
functions. For that same reason, you won't be able to export slackUserNames
as a variable, as it depends on previously executing asynchronous code.
A straightforward approach would be to export an asynchronous function from your file that you could use in other parts of your project then:
import { WebClient } from '@slack/web-api';
import { prSummaryBotEnvVariables } from '../pr-summary-bot-env/env-exported-variables.js';
import { getUsersFirstAndLastName, removeUndefinedValuesFromArray } from '../utils/utils.js';
export async function getSlackUserNames() {
const botToken = prSummaryBotEnvVariables();
const client = new WebClient(botToken.botUserOauthToken);
let slackUserEmails;
const client = new WebClient(botToken.botUserOauthToken);
let slackUserEmails;
try {
const {members} = await client.users.list();
slackUserEmails = members.map(member => member.profile.email);
} catch (error) {
console.error(error);
}
const slackUserNames = removeUndefinedValuesFromArray(getUsersFirstAndLastName(slackUserEmails));
}
Now you could call this function somewhere else in your project:
const slackUserNames = await getSlackuserNames();
console.log("Slack users are: ", slackUserNames);