bluebird-tools
v1.8.0
Published
Tools to improve the bluebird promises with control flow and logging.
Downloads
6
Maintainers
Readme
Bluebird Tools
Tools to improve the bluebird promises with control flow and logging.
Install
npm install --save bluebird-tools
How to use
const Promise = require('bluebird-tools');
Control Flow
Promise.iif(condition, success, fail) -> promise
Calls success
if pass the condition
or calls fail
Promise.iif(x => x === 1, x => console.log('success', x), x => console.log('fail', x));
promise.iif(condition, success, fail) -> promise
Calls success
if pass the condition
or calls fail
Promise.resolve(1)
.iif(x => x === 1, x => console.log('success', x), x => console.log('fail', x));
promise.for(start, end, method) -> promise
Calls method
passing the iterator
and the resolved value
Promise.resolve(123)
.for(0, 5, (i, val) => console.log('iterator:', i, 'value:', val));
/* output:
iterator: 0 value: 123
iterator: 1 value: 123
iterator: 2 value: 123
iterator: 3 value: 123
iterator: 4 value: 123
*/
promise.when(condition, success) -> promise
Calls success
if pass the condition
or calls fail
Promise.resolve(1)
.when(x => x === 1, x => console.log('success', x));
Promise.when(condition, success) -> promise
Calls success
if pass the condition
or calls fail
Promise.when(x => x === 1, x => console.log('success', x));
promise.unless(condition, fail) -> promise
Calls success
if pass the condition
or calls fail
Promise.resolve(1)
.unless(x => x === 2, x => console.log('fail', x));
Promise.unless(condition, fail) -> promise
Calls success
if pass the condition
or calls fail
Promise.unless(x => x === 2, x => console.log('fail', x));
promise.thenMonitor(name, method) -> promise
Calls method
monitoring starting and ending
Promise.resolve()
.thenMonitor('something', () => executeSomething());
// logs:
// - starting something
// - finishing something - 65.564ms
promise.whenMonitor(name, conditional, method) -> promise
If conditional
is true, calls method
monitoring starting and ending
Promise.resolve(3)
.whenMonitor('something', x => x === 3, () => executeSomething());
// logs:
// - starting something
// - finishing something - 1.234ms
promise.unlessMonitor(name, conditional, method) -> promise
If conditional
is false, calls method
monitoring starting and ending
Promise.resolve(3)
.unlessMonitor('something', x => x === 2, () => executeSomething());
// logs:
// - starting something
// - finishing something - 1.234ms
promise.iifMonitor(name, conditional, method) -> promise
If conditional
is true, calls success
or, if is false, calls fail
, monitoring starting and ending
Promise.resolve(3)
.iifMonitor('something', x => x === 3,
() => executeSomething(), () => executeSomethingElse());
// logs:
// - starting something
// - process something has success
// - finishing something - 1.234ms
Promise.monitor(name, method) -> promise
Calls method
, monitoring starting and ending
Promise.monitor('something', () => executeSomething());
// logs:
// - starting something
// - finishing something - 1.234ms
Logging
Configure logging for all Promise with the logging
function.
Default levels to call the log:
- silly
- debug
- verbose
- info
- warning
- error
Promise.configureLog(logging)
const winston = require('winston');
// configure winston
Promise.configureLog(function logging(level, text, ...args) {
winston.log(level, text, ...args);
});
Promise.log(level, text, ...args) -> promise
Promise.log('info', 'testing log', 1, 2, 3);
promise.log(level, text, ...args) -> promise
Promise.resolve().log('info', 'testing log', 1, 2, 3);
promise.silly(text, ...args) -> promise
Promise.resolve().silly('testing log', 1, 2, 3);
promise.debug(text, ...args) -> promise
Promise.resolve().debug('testing log', 1, 2, 3);
promise.verbose(text, ...args) -> promise
Promise.resolve().verbose('testing log', 1, 2, 3);
promise.info(text, ...args) -> promise
Promise.resolve().info('testing log', 1, 2, 3);
promise.warning(text, ...args) -> promise
Promise.resolve().warning('testing log', 1, 2, 3);
promise.error(text, ...args) -> promise
Promise.resolve().error('testing log', 1, 2, 3);
promise.whenLog(level, conditional, text, ...args) -> promise
Promise.resolve(1).whenLog('into', x => x === 1, 'testing log', 1, 2, 3);
promise.unlessLog(level, conditional, text, ...args) -> promise
Promise.resolve(1).unlessLog('into', x => x === 2, 'testing log', 1, 2, 3);
Manipulating promises
Promise.convert(promise) -> promise
Converts a native promise to a BluebirdTools promise
Promise.convert(promise)
promise.isBluebird -> bool
Converts a native promise to a BluebirdTools promise
if (Promise.resolve().isBluebird) { // true
}