I have been trying to get the result of this asynchronous function to no degree of success for the past few hours. I have checked the past similar questions and solutions but nothing has worked for this specific case.
This particular code below logs the value when I run console.log:
const okay = async () => {
console.log(await getTimezoneLabel(timezone));
};
okay();
The code below logs Promise {pending}
instead of a value to the console which is baffling
const okay = async () => {
return await getTimezoneLabel(timezone);
};
let result = okay();
console.log(result);
Here is the getTimezoneLabel function itself:
async function getTimezoneLabel(timezone) {
const timezoneObject = await convertTimezone('original', timezone);
return timezoneObject[0].label;
}
Here is the convertTimezone function which the getTimezoneLabel function references:
import timezonesList from '../timezone-list';
async function convertTimezone(labelStyle, timezone) {
const spacetime = (await import('spacetime')).default;
const soft = (await import('timezone-soft')).default;
const timezones = Object.fromEntries(Object.entries(timezonesList).filter(([key]) => key.includes(timezone)));
return Object.entries(timezones)
.reduce((selectOptions, zone) => {
const now = spacetime.now(zone[0]);
const tz = now.timezone();
const tzStrings = soft(zone[0]);
let label = '';
let abbr = now.isDST()
? // @ts-expect-error
tzStrings[0].daylight?.abbr
: // @ts-expect-error
tzStrings[0].standard?.abbr;
let altName = now.isDST() ? tzStrings[0].daylight?.name : tzStrings[0].standard?.name;
const min = tz.current.offset * 60;
const hr = `${(min / 60) ^ 0}:` + (min % 60 === 0 ? '00' : Math.abs(min % 60));
const prefix = `(GMT${hr.includes('-') ? hr : `+${hr}`}) ${zone[1]}`;
switch (labelStyle) {
case 'original':
label = prefix;
break;
case 'altName':
label = `${prefix} ${altName?.length ? `(${altName})` : ''}`;
break;
case 'abbrev':
label = `${prefix} ${abbr?.length < 5 ? `(${abbr})` : ''}`;
break;
default:
label = `${prefix}`;
}
selectOptions.push({
value: tz.name,
label: label,
offset: tz.current.offset,
abbrev: abbr,
altName: altName,
});
return selectOptions;
}, [])
.sort((a, b) => a.offset - b.offset);
}
How can I get this to work?
Thank you so much in advance.