1

So I have an array where I need to filter it from undefined values and then use it (please see the following example!) so since filter method is async there is no guarantee that results array is filtered where I'm logging it!

//Here I need to filter the results from undefined!
results = results.filter(function (item) {
    return item !== undefined;
});
//But since the filter method is async, it is not guaranteed that results is filtered here yet!
console.log(results)
Rodrigo Medeiros
  • 7,814
  • 4
  • 43
  • 54
  • Use [async.filter()](https://www.npmjs.com/package/async#filter). – Rodrigo Medeiros Mar 03 '15 at 15:57
  • 5
    The standard [`.filter()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) does work synchronously. Unless something within the `function` provided to it is asynchronous, the results will be available for the next line of code. – Jonathan Lonowski Mar 03 '15 at 15:58
  • @JonathanLonowski So you mean my `results.filter` in my code runs `synchronously` although I have a callback function in it? –  Mar 03 '15 at 16:02
  • 4
    @user3421904 Yes. Just because a method takes a function does not make it asynchronous. If that `filter()` method is the standard `Array.prototype.filter` method it will be called synchronously for each element in the array. – mbcrute Mar 03 '15 at 16:05
  • @JonathanLonowski This may be useful in future for other people, please feel free to answer the question and I mark it as solved –  Mar 03 '15 at 16:13

0 Answers0