Skip to content

Instantly share code, notes, and snippets.

@kriskowal kriskowal/promise-abort.js

Last active Feb 29, 2020
What would you like to do?
var DIR = '/tmp/test',
FILE_PATTERN = /test/,
fs = require('q-io/fs');
.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;

This comment has been minimized.

Copy link

shrikrishnaholla commented Apr 9, 2017

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
You can’t perform that action at this time.