Last active
February 29, 2020 04:35
-
-
Save kriskowal/e98774443eb0f1653871 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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