独占访问 mapLimit return 中的变量

Exclusive access to variable in mapLimit return

我是 nodejs 编程的新手,但不是编程的新手 (C/C++/Python/Shaders),我有一个关于独占访问全局变量的问题,例如async.mapLimit return 它的回调

例子

var myGlobalCounter = 0;

function executeDownload(item, callback){

  exec('./ascriptthatdownload.sh ' + item, function (error, stdout, stderr) {
    // Here do I have exclusive access to myGlobalCounter
    // so that I could do this or update lets say a UI component?
    myGlobalCounter++
    console.log('Downloads ready:', myGlobalCounter);
  });   
}

function downloadSomeFiles() {
  var listOfFiles = [];
  // create download links
  async.mapLimit(listOfFiles, 4, executeDownload, function(err, results){

  });
}

我可以让它工作,但我不知道这是否足够安全?其他建议也表示赞赏。在 C/C++ 中,我会使用互斥锁来防止同时访问 myGlobalCounter。

编辑:我希望能够在每次下载准备就绪时安全地将 myGlobalCounter 计数为 1,然后将其传递给 console.log 或传递给另一个组件

赞扬@CertainPerformance 在他的评论中的解释(我不能接受作为答案)

Yes, Javascript is single-threaded - a synchronous block of code will run to the end before any other callback can run, look up the event loop. You almost never have to worry about shared mutable state in JS