Callback and abort


#1

I’m using a process that the user can abort by making another choice in the browser. But I’m not sure how to handle it properly.

Basically I use a sync readdir, process the files by extracting them, update the browser with the extracted info. But part way through the process the user may have selected another path. This should abandon the current path that is processing and then start on the new one they selected.

pseudo code

$button.on('click', function() { // <-- So abort if running when this is clicked
  readfiles($this.data-id);
});

function readfiles(path) {
  fs.readdir(path, function(files) {
    files.forEach(function(file) {
      process(file);
    }
  }
}

function process(file) {
  // Do stuff synchronously with the file
  dothis(callback(file) {
    // Do stuff
    emitter.emit('processed', file);
  });
}

emitter.on('processed', { (args) => {
  $div.append(args);
});

#2

Brainfart: I actually should have typed ASYNC