I'm using a Promise chain to write some data to a file, execute a command on it, inspect its results, and send the results, using Express.js. The thing about my function is that I need to be able to exit early from multiple conditions.The following code comes from an Express.js handler. my-command is a command that takes one input file, and outputs either resultWithBadName.txt, when the output is only one page, and result-{id}.txt, when the output has multiple pages.var cp = require('child_process'),    Promise = require('bluebird');var results,    id = req.param.id; // from userpromisifiedWriteFile('src.txt', 'foobar' /* real content from user */).catch(function (err) {    return Promise.reject({ text: 'Cannot write file', err: err });}).then(function() {    return exec(        'my-command src.txt'    ).catch(function (err) {        // Considered a user error.        res.send({            error: err.stderr,            id: id,            pages: 0        });        return Promise.reject('DONE');     // UGLY    });}).then(function (ret) {    results = ret;    // Check if output only has one page    return promisifiedStat(        'resultWithBadName.txt'    ).catch(function () {        // Nope, has multiple pages. Coutn how many pages there are.        return countPagesInOutput(id).then(function (pages) {            res.send({                output: results.stderr,                id: id,                pages: pages            });            return Promise.reject('DONE'); // UGLY        });    });}).then(function () {    // Yep, only has one page. Let's make the result have standard naming scheme.    return promisifiedRename(        'resultWithBadName.txt',        'result' + id + '.txt'    ).catch(function (err) {        return Promise.reject({ text: 'file rename failed', err: err });    });}).then(function () {    res.send({        output: results.stderr,        id: id,        pages: 1    });}).catch(function (err) {    if (err === 'DONE') return;         // UGLY    res.status(500).send('Internal server error: ' +        (err.text || err.message || '')    );    console.error(err.err || err);}).catch(console.error);function exec() {    var args = [].slice.apply(arguments);    return new Promise(function (fulfill, reject) {        args.push(function (err, stdout, stderr) {            if (err) {                err.stderr = stderr;                return reject(err);            }            fulfill({                stdout: stdout,                stderr: stderr            });        });        cp.exec.apply(cp, args);    });};I have read somewhere that one can chain the thens inside a onFulfilled handler, but that adds even more pyramids to my code, which Promises are supposed to preventNow my question is, is there a way to remove the ugly 'DONE' hack?
Exiting a Promise early
javascript;node.js;express.js