Skip to content Skip to sidebar Skip to footer

Async Await Does Not Work As Expected

Currently we are storing short strings as keys. These keys correspond to long values which are labels. I am trying to update the corresponding long value for the key. But the cons

Solution 1:

Your forEach is using an async function which means that the loop probably finishes before any of the promises it created do. To fix this, you need to get the result of your promises and wait on them. However, this means the enclosing function must itself be async, which may or may not be acceptable for your actual use case.

export const getRadioValues = async (record: IRecordInput) => {
    const singleSelectKeys = ['Race', 'DeathWas', 'MannerOfDeath'];
    await Promise.all(singleSelectKeys.map(async key => {
        if (record[key]) {
            const dropDownOption = await DropDownOptions.find({ where: { id: record[key] }}) as IPDFSelect;
            record[key] = dropDownOption.dataValues.Text;
            console.log(record[key]);
        }
    }));
    console.log(record);
    return record;
};

Post a Comment for "Async Await Does Not Work As Expected"