Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Last active February 29, 2020 04:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskowal/e98774443eb0f1653871 to your computer and use it in GitHub Desktop.
Save kriskowal/e98774443eb0f1653871 to your computer and use it in GitHub Desktop.
var DIR = '/tmp/test',
FILE_PATTERN = /test/,
fs = require('q-io/fs');
fs.list(DIR)
.then(function (files) {
var matched = files.filter(function (item) {
return item.match(FILE_PATTERN);
});
if (matched.length !== 1) {
var error = new Error("Can't delete nonce because " + matched.length + " entries found instead of merely 1");
error.nonce = true;
throw error;
}
return fs.remove(fs.join(DIR, matched[0]));
}, function (cause) {
if (cause.code === "ENOENT") {
var error = new Error("Can't delete nonce because directory not found");
error.nonce = true;
} else {
throw cause;
}
})
.catch(error) {
if (error.nonce) {
// Handle above cases
// ....
} else {
throw error;
}
})
@shrikrishnaholla
Copy link

I was finding a way to abort a promise chain too and found this from StackOverflow. I had ended up with the same idea as well, but I'm using error.intentional as the boolean variable to check in the error handler. I wish there were a cleaner approach but oh well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment