Async functions not waiting inside a forEach loop

Viewed 1267


I have a call to my async function inside a forEach loop, like this:

foo {
    list.forEach(function( field ) {        
        populateValues( field );
    });

    // here my list is returned incomplete
    return list;
}

populateValues = async function ( field ) {
    if ( field.someProp === true ) {
        fields.val = await somePromise();
    }
}

somePromise = function() {
    return new Promise( resolve => {
        fetchMyAPIExample().then( function( value ) {
            resolve( value );
        }
    }
}

populateValues() waits correctly my promise, but foo() doesn't wait populateValues to return the list, so it return my list incomplete.

2 Answers
Related