All files / node-user-accounts-boilerplate-nahid/helper resolve.js

100% Statements 3/3
77.78% Branches 7/9
100% Functions 2/2
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21                          10x   9x       5x  
"use strict";
 
/**
 * Monitors a Promise and returns success or error based on promise outcome.
 * 
 * @param {Response} res use as res.resolve(...)
 * @param {Promise} promiseObj Promise objecy to monitor
 * @param {string|object} [message=undefined] success or error message to return; if this is not specified, result of promise is returned.
 * @param {string} [audit=false] audit event type. note that '_SUCCESS' or '_FAILURE' is attached to this type depenidng on outcome
 * @param {string} [extra=undefined] extra audit payload which may help debug event
 */
function resolve(res, promiseObj, message = undefined, audit = false, extra = undefined)
{
  promiseObj.then((response) =>
  {
    res.success(message || response, audit && `${audit}_SUCCESS`, extra);
  }, res.reject(audit && `${audit}_FAILURE`, extra));
}
 
module.exports = resolve;