silently v1.1.4
silently
silently
is a function that is used to invoke another function that you want to fail silently (meaning that any errors that it throws must
be suppressed). The main motivation behind it was to allow me to be able to do the following...
Promise.all([
doSomething(),
silently(doSomeOtherThing)
])
.then(results => {
console.log(results[0]);
console.log(results[1]);
});
Promise.all
will normally reject if one of the promises rejects and I found myself in situations when I didn't want it
to reject because one promise rejected. In the above example, if doSomeOtherThing()
returns a promise that rejects, or if it throws an error,
the error would be handled silently and undefined
would be logged to the console for result[1]
.
Installation
$ npm install silently --save
Usage
function doSomething() {
throw new Error('something went wrong');
}
var result = silently(doSomething); // result is undefined
Or with promises...
function doSomethingAsynchronously() {
return new Promise((resolve, reject) => {
reject(new Error('something went wrong'));
})
}
silently(doSomethingAsynchronously)
.then(result => {
console.log(result); // undefined will be logged to the console
})
If you want to pass args to the function, you can do the following
function doSomething(arg1, arg2) {
return arg1 + ' ' + arg2;
}
var result = silently
.call(doSomething)
.boundTo(null, 'hello', 'world')
.asap(); // result will be 'hello world'
This is just a more fluent way of doing silently(doSomething.bind(null, 'hello', 'world');
Sugar
The following sugar is also supported, which is also the same as doing silently.call()...
silently.called(action)...
silenlty.do(action)...
Running the tests
make install
make dev
make test