0

I have a scope problem when populating an array with a function (code of getimages-altbis.js in the picture): array is populated locally to the function, not the global var.

(function() {
  var a = [];

  function parse() {
    const parser = require('rss-url-parser');
    parser('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')
      .then((data) => photoSet(data));
  }

  function photoSet(items) {
    a.push(items);
  };

  console.log(a.length);
})();

Any idea on how to populate this array from the parse()?

Thanks.

  • 1
    Include a [mcve] in your question. Don't post links to pictures of code. – Quentin Dec 24 '16 at 16:51
  • Why `popA()` function is actually pushing into `a` array..? Also in the right snippet you are trying to `console.log(a.length)` synchronously while it gets populated asynchronously. You should expect to see `0` on the console unless you carry the `console.log(a.length)` instruction either to inside the first `.then` stage or chain up a separate `.then` stage for it. – Redu Dec 24 '16 at 16:56
  • Also, be consistent with `var`, `let`, and `const`. – nicovank Dec 24 '16 at 17:11
  • @Quentin Thanks for the comment, just moved the code into the message. – user7338039 Dec 25 '16 at 11:13

0 Answers0