Callback and abort


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

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

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

emitter.on('processed', { (args) => {


Brainfart: I actually should have typed ASYNC