In my code, based on a specific condition, I would like to skip to the done
function, irrespective of all the then
functions.
The original version of this question is in the edits. The following is the actual problem I am dealing with. Sorry for the inconvenience
Actual Problem :
I am reading a file and processing it. If the contents of the file match certain conditions, I have to do a series of operations on the file system (say read and write few files) and then to execute done
function. If the conditions fail, I have to skip all the series of operations and I have to execute the done
function directly.
I return an object (lets say result
) in all the then
functions and in the next then
I update result
and return it. So, when all the then
are done, done
will have the accumulated result
. Finally, the done
will process result
and print it.
So, if the conditions are not met initially, done
will simply print result
(which would be empty).
Q()
.then(readFile)
.then(function (contents) {
var processResult = process the contents;
if (processResult) {
return {};
} else {
// How can I skip to `done` from here
}
})
.then(function (results) {
// do some more processing and update results
return results;
})
... // Few more then functions similar to the one above
...
.fail(function (exception) {
console.error(exception.stack);
})
.done(function (results) {
// do some more processing and update results
console.log(results);
});