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.resolve(1)
.iif(x => x === 1, x => console.log('success', x), x => console.log('fail', x));
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.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.thenMonitor(name, method) -> promise
Calls method
monitoring starting and ending
Promise.resolve()
.thenMonitor('something', () => executeSomething());
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());
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());
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());
Promise.monitor(name, method) -> promise
Calls method
, monitoring starting and ending
Promise.monitor('something', () => executeSomething());
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');
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) {
}