0

I have three javascript functions.

rerender(callbackOptional);
releaseEMail_inFolders(eMailId, currentEMailFolderId, callbackOptional);
attachEMail_inFolders(eMailId, futureEMailFolderId, callbackOptional);

I have this variables:

  • eMailId = 123
  • currentEMailFolderId = 100
  • futureEMailFolderId = [101,102]

The eMail 123 is currently in the folder 100. I like to release the eMail from the folder 100 and I like to attach the eMail into folders 101 and 102. Then rerender().

Usually I would write:

attachEMail_inFolders(123, 101);
attachEMail_inFolders(123, 102);
releaseEMail_inFolders(123, 100);
rerender();

Now I tried to use the callbacks in an array (chain) using this code:

var chain=[];
chain.push(rerender);
chain.push(()=>releaseEMail_inFolders(eMailId, currentEMailFolderId, chain.pop()));
for(var i =0; i < futureEMailFolderIds.length;i++) {
    var v = futureEMailFolderIds[i];
    if (typeof v == 'number'){
        chain.push(()=>{
            console.log(v);
            attachEMail_inFolders(eMailId, v, chain.pop());
        });
    }
}
chain.pop()();

Unfortunately I get this error-message in chrome:

 null
 Uncaught futureEMailFolderId of attachEMail_inFolders is undefined!

But in firefox I get this message:

 102
 101

Why does chrome not allow to use variables exclusievly from this scope to be used inside lamdas/functions?

Grim
  • 1,938
  • 10
  • 56
  • 123
  • Scope won't be the problem, but maybe timing of operations. I think your best bet is setting break points/add more detailed `console.log` messages to see what's happening when. – Felix Kling Feb 21 '22 at 12:35
  • @FelixKling In chrome or in firefox? – Grim Feb 21 '22 at 12:39
  • 1
    Both? It's certainly weird that you get different results. But I can already tell you that `v` won't have the value you expect it to have (see [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/q/750486/218196)). Maybe your problem already solves itself by using `let` declarations instead of `var` (so in a sense, scope might actually be the problem, but variable visibility is not (and there is no difference between browsers in that regard)). – Felix Kling Feb 21 '22 at 12:42

1 Answers1

0

Was indeed a scope problem. Lamdas ignore vars (in chrome only).

I modified the code to avoid lamdas but use functions.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • *"Lamdas ignore vars (in chrome only)."* This is not the explanation for what you are experiencing, or at least it's a very vague description. Scope doesn't work differently in Firefox and Chrome. – Felix Kling Feb 21 '22 at 20:06
  • So its at least vague that its "not the explanation" vice versa. Do not hasitate to correct me. :) Some ppl are fishing for correction, well I dont. I solved the problem by tranform all lamdas into functions and it works in all browsers (at least mozilla and chrome/chromium). I have one advantage that sticks me to functions: Id like to be browser-independend. And functions instead of lamdas are a good bet for me and for old browsers as well. – Grim Feb 21 '22 at 23:08
  • I'm happy that you fixed your problem, but IMO as it stands, this question/answer is of little help to anybody else because we don't actually know what caused the problem (and we can reproduce the issue with the information you provided). – Felix Kling Feb 22 '22 at 05:58