Repository URL to install this package:
|
Version:
3.12.2 ▾
|
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/**
* Wraps the promise passed with try/catch block and fires the callback provided depending on success/failture.
*
* @param (promise: promise) - promise to be processed
* @param (successCallback: function) - callback function to be implemented incase of success
* @param (errorCallback: function) - callback function to be implemented incase of error caught
- assuming default clbk function would be info(error: error instance, type: error, timeout: 5000)
* @param (finishCallback: function) - callback function to be implemented after finishing in both cases success/fail.
*/
var handlePromise = function handlePromise(promise) {
var successCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {
return null;
};
var errorCallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {
return null;
};
var finishCallback = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
return null;
};
if (promise instanceof Promise) {
// To throw the rejection of the promise to be able to use (.catch)
if (typeof promise.throwRejection === 'function') {
promise.throwRejection();
}
var newPromise = promise.then(successCallback)["catch"](function (error) {
if (typeof errorCallback === 'function') {
errorCallback(error, 'error', 5000);
}
})["finally"](finishCallback);
// To copy the properties of the original promise eg. if it's thunk function we will have (abort, requestId...etc.)
_extends(newPromise, promise);
return newPromise;
}
};
export default handlePromise;