UNPKG

@garcia-s/tshelpers

Version:

Helper functions and Data structures for nodejs

54 lines (53 loc) 1.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.pcall = pcall; exports.pcallSync = pcallSync; /** * Helper function for async error handling * * Returns an [Array] with two values [true] and [T] (The result of the callback promise) * Or [false] and an instance of the [Error] thrown by the function * * @remarks * For non async functions please use [pcallSync] * **/ function pcall(func) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield func(); return [true, result]; } catch (error) { return [false, error]; } }); } /** * Helper function for error handling * * Returns an [Array] with two values [true] and [T] (The result of callback function) * Or [false] and an instance of the [Error] thrown by the * * @remarks * For async functions please use [pcall] * **/ function pcallSync(func) { try { const result = func(); return [true, result]; // Successful execution } catch (error) { return [false, error]; // Catch any error } }