Async Await does not work as expected [duplicate]

2019-08-25 13:36发布

问题:

This question already has an answer here:

  • Using async/await with a forEach loop 14 answers

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 console.log(record) always executes first and then the inner log statement executes which is not is desired. It always sends the unmodified record to the getRadioValues function caller.

I want to return the record after the corresponding key is updated.

export const getRadioValues = (record: IRecordInput) => {
    const singleSelectKeys = ['Race', 'DeathWas', 'MannerOfDeath'];
    singleSelectKeys.forEach(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;
};

回答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;
};