最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Async functions not waiting inside a forEach loop - Stack Overflow

programmeradmin3浏览0评论


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 inplete
    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 inplete.


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 inplete
    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 inplete.

Share Improve this question edited Aug 1, 2017 at 17:49 gustavohenke 41.5k14 gold badges125 silver badges132 bronze badges asked Aug 1, 2017 at 14:43 Elzio JrElzio Jr 737 bronze badges 5
  • 1 anyway, nothing here modifies the var list, so correcting this won't be enough, your list will stay the same. We also have a reference to fields that is not declared here – Kaddath Commented Aug 1, 2017 at 14:47
  • @kaddath no, field.val = ... – Jonas Wilms Commented Aug 1, 2017 at 14:50
  • @Jonasw i guess this is what was intended, but this won't really help too, as field in his code is a string with the property name, not the property itself. But you corrected allright in your answer i guess – Kaddath Commented Aug 1, 2017 at 14:58
  • @kaddath no, thats not jquerys forEach. – Jonas Wilms Commented Aug 1, 2017 at 15:10
  • ah yes, you're right ^^ should be careful about that in the future! – Kaddath Commented Aug 1, 2017 at 15:12
Add a ment  | 

2 Answers 2

Reset to default 7

You may want to await there too, which doesnt work with forEach but with for..of :

async function foo(){
  for(var field of list){        
    await populateValues( field );
  }
  return list
}

Or if you want to enable racing:

function foo(){
  return Promise.all(
    list.map( field => populateValues( field ))
  ).then(_=>list);
}

Being that each of the function calls to populateValues() is async, the function foo doesn't wait to return the list object.

You can make foo await the results of populateValues to get the response you expect.

发布评论

评论列表(0)

  1. 暂无评论