0

I want to push on my array all the data which are on the feed variable. I need to access to the data array variable outside of the foreach loop. But the console.log is execute before the loop. I have try to change the promise to an async await and it is the same.

let data = []

sources.forEach((src) => {
    parser.parseURL(src.rss, (err, feed) => {
        data.push(feed)
    })
})

console.log(data)

Thank you to you help.

ljahier
  • 1
  • 1
  • Does this answer your question? [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – Daniel_Knights Dec 23 '20 at 20:02

1 Answers1

0

I could make an ad-hoc debounce function.. debounce as in it'll run n milliseconds AFTER it's finished calling.. surprisingly, 0 milliseconds would still occur after an instruction written below it.. using that logic, here's an example

var debObj={}; //empty object SOLELY for temporary storage of the debounce function
function debounce(fn,n,obj){ //debounce function
  function doIt(){
    obj[fn.toString()]=[fn,setTimeout(()=>{
      fn();delete(obj[fn.toString()])
    },n)]
  }
  if(obj[fn.toString()]){
    clearTimeout(obj[fn.toString()][1])
    doIt(); return;
  }
  doIt(); return;
}
function debouncer(fn,n){return function(){debounce(fn,n,debObj)}} //debounce function that imitates the functionality of the real debounce
//now for your code snippet...............................................................
let data = []
let logIt=debouncer(()=>console.log(data),0)
sources.forEach((src) => {
    parser.parseURL(src.rss, (err, feed) => {
        data.push(feed)
    })
    logIt()
})

I know it's extremely adhoc.. but does it work?

The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17