-
Notifications
You must be signed in to change notification settings - Fork 9
Tips and Tricks
If you are ever in a situation where a value is either an error value or null
, you can throw it without bothering to check for null
first.
task (x, y) {
...
var err = fnThatReturnsNullOrError(hey, ho);
throw err;
// continue with other code.
}
A drawback with NodeJS style callbacks is that an error value of "null" is by convention considered not an error. It therefore makes no sense to flag an error if the error value is null or falsy. The throw
statement in cspjs exploits this by not throwing anything falsy. In code, throw err;
is equivalent to if (err) { throw err; }
.
This situation can occur when working with a merging channel, for example. The values coming out of a channel created by Channel.merge
are objects that provide more info about which channel produced the value, and whether any error occurred. If your task is to be considered failed if any channel sends an error, you can just do this -
var ch = Channel.merge([ch1, ch2, ch3]);
x <- chan ch;
throw x.err;
// continue with other stuff if no error was sent.