0

I am a little confused about scope in javascript.

 async function isNth(page, number){
    let peeps = await page.evaluate(() =>
        {
            console.log('undefined: '+number)
        }
    );
 }

I'm using puppeteer, but my problem lies with a lack of understanding of how javascript treats method params, as above, which are simply unavailable inside a "callback?", at least I think that's what that's called, or maybe it's "closure"-

HellishHeat
  • 2,280
  • 4
  • 31
  • 37
  • do [this question](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) helps ? – jonatjano Aug 17 '20 at 08:43
  • https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Teemu Aug 17 '20 at 08:46
  • https://javascript.info/closure may be helpful. – Cat Aug 17 '20 at 08:55
  • 2
    *which are simply unavailable inside a "callback?"* — What makes you think they are unavailable? – Quentin Aug 17 '20 at 08:56
  • I vaguely seem to remember that something about puppeteer was very special with this kind of thing, but it's not my area of expertise. Some more context might help pin this down… – deceze Aug 17 '20 at 08:58

1 Answers1

0

Should not be a problem with arrow functions as demonstrated here. Are you sure that the number isn't already undefined when passed to isNth?

const someOtherFunction = (callback) => {
  callback();
}

function isNth(page, number){
  let peeps = someOtherFunction(() => {
    console.log('undefined: ' + number)
  });
}

isNth(0,1);
MauriceNino
  • 6,214
  • 1
  • 23
  • 60