Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

silent-promise

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

silent-promise - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

dist/src/silent-promise-error.d.ts

36

dist/index.d.ts

@@ -6,21 +6,21 @@ export { promisify } from "util";

}
export declare const bind: unique symbol;
export declare function bindTo(thisArg: any): any;
export declare type Callback<T> = (error: any | undefined, result?: T) => void;
declare type Args = any[];
declare type RestArgumentFunction = (...args: Args) => void;
declare type SilentPromiseFunction<T> = (...args: Args) => SilentPromise<T>;
export declare type Args = any[];
export declare type RestArgumentFunction = (...args: Args) => void;
export declare type SilentPromiseFunction<T> = (...args: Args) => SilentPromise<T>;
export declare function silentPromise<T>(promise: Promise<T>): SilentPromise<T>;
export declare const runSilentCallback: typeof runFunctionWithArguments;
export declare function silentCallback<T>(fn: (callback: Callback<T>) => void): () => SilentPromise<T>;
export declare function silentCallback<A1, T>(fn: (arg1: A1, callback: Callback<T>) => void): (arg1: A1) => SilentPromise<T>;
export declare function silentCallback<A1, A2, T>(fn: (arg1: A1, arg2: A2, callback: Callback<T>) => void): (arg1: A1, arg2: A2) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, T>(fn: (arg1: A1, arg2: A2, arg3: A3, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10) => SilentPromise<T>;
export declare function silentCallback<T>(fn: RestArgumentFunction): SilentPromiseFunction<T>;
export declare function silentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;
declare function runFunctionWithArguments<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;
export declare function silentCallback<T>(fn: (callback: Callback<T>) => void, bind?: any): () => SilentPromise<T>;
export declare function silentCallback<A1, T>(fn: (arg1: A1, callback: Callback<T>) => void, bind?: any): (arg1: A1) => SilentPromise<T>;
export declare function silentCallback<A1, A2, T>(fn: (arg1: A1, arg2: A2, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, T>(fn: (arg1: A1, arg2: A2, arg3: A3, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10) => SilentPromise<T>;
export declare function execSilentCallback<T>(fn: RestArgumentFunction, bind: any): SilentPromiseFunction<T>;
export declare function execSilentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;

@@ -14,2 +14,9 @@ "use strict";

exports.SilentPromise = SilentPromise;
exports.bind = Symbol("_silent_promise_callback_bind_arg");
function bindTo(thisArg) {
const bindArg = {};
bindArg[exports.bind] = thisArg;
return bindArg;
}
exports.bindTo = bindTo;
function silentPromise(promise) {

@@ -24,17 +31,42 @@ const p = promise;

exports.silentPromise = silentPromise;
exports.runSilentCallback = runFunctionWithArguments;
function silentCallback(fn, ...args) {
if (args.length === 0) {
return wrapFunction(fn);
// above functions into 1 - wraps fn into a function that returns SilentPromise (SilentPromiseFunction)
function silentCallback(fn, bind) {
const wrapFn = getSilentPromiseFunction(fn, getBindArgument(bind) || bind);
console.log(bind);
Object.setPrototypeOf(wrapFn, Object.getPrototypeOf(fn));
return Object.defineProperties(wrapFn, Object.getOwnPropertyDescriptors(fn));
}
exports.silentCallback = silentCallback;
function execSilentCallback(fn, ...args) {
const bindArgs = getBindArgument(...args);
if (bindArgs) {
return getSilentPromiseFunction(fn, bindArgs);
}
else {
return runFunctionWithArguments(fn, ...args);
return new Promise((resolve) => {
// append callback to intended function call
args.push((error, result) => {
if (error) {
resolve([new promise_error_1.default(error), undefined]);
}
else {
resolve([new promise_error_1.default(), result]);
}
});
fn.call(this, ...args);
});
}
}
exports.silentCallback = silentCallback;
function wrapFunction(fn) {
const wrapFn = (...args) => {
exports.execSilentCallback = execSilentCallback;
function getBindArgument(...args) {
// must be the only argument
if (args.length === 1 && args[0] instanceof Object && args[0][exports.bind]) {
return args[0][exports.bind];
}
}
function getSilentPromiseFunction(fn, bindArg) {
return (...args) => {
return new Promise((resolve) => {
try {
fn.call(this, ...args, (error, result) => {
fn.call(bindArg ? bindArg : this, ...args, (error, result) => {
if (error) {

@@ -53,19 +85,3 @@ resolve([new promise_error_1.default(error), undefined]);

};
Object.setPrototypeOf(wrapFn, Object.getPrototypeOf(fn));
return Object.defineProperties(wrapFn, Object.getOwnPropertyDescriptors(fn));
}
function runFunctionWithArguments(fn, ...args) {
return new Promise((resolve) => {
// append callback to intended function call
args.push((error, result) => {
if (error) {
resolve([new promise_error_1.default(error), undefined]);
}
else {
resolve([new promise_error_1.default(), result]);
}
});
fn.call(this, ...args);
});
}
//# sourceMappingURL=index.js.map
export { promisify } from "util";
export { SilentPromiseError } from "./silent-promise-error";
import PromiseError from "./promise-error";
export declare class SilentPromise<T> extends Promise<[PromiseError, T | undefined]> {
}
export declare class SilentPromiseError {
error: any;
message: string;
constructor(error: any, message: string);
}
export declare const bind: unique symbol;
export declare function bindTo(thisArg: any): any;
export declare type Callback<T> = (error: any | undefined, result?: T) => void;
declare type Args = any[];
declare type RestArgumentFunction = (...args: Args) => void;
declare type SilentPromiseFunction<T> = (...args: Args) => SilentPromise<T>;
export declare type Args = any[];
export declare type RestArgumentFunction = (...args: Args) => void;
export declare type SilentPromiseFunction<T> = (...args: Args) => SilentPromise<T>;
export declare function silentPromise<T>(promise: Promise<T>): SilentPromise<T>;
export declare const runSilentCallback: typeof runFunctionWithArguments;
export declare function silentCallback<T>(fn: (callback: Callback<T>) => void): () => SilentPromise<T>;
export declare function silentCallback<A1, T>(fn: (arg1: A1, callback: Callback<T>) => void): (arg1: A1) => SilentPromise<T>;
export declare function silentCallback<A1, A2, T>(fn: (arg1: A1, arg2: A2, callback: Callback<T>) => void): (arg1: A1, arg2: A2) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, T>(fn: (arg1: A1, arg2: A2, arg3: A3, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10) => SilentPromise<T>;
export declare function silentCallback<T>(fn: RestArgumentFunction): SilentPromiseFunction<T>;
export declare function silentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;
declare function runFunctionWithArguments<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;
export declare function silentCallback<T>(fn: (callback: Callback<T>) => void, bind?: any): () => SilentPromise<T>;
export declare function silentCallback<A1, T>(fn: (arg1: A1, callback: Callback<T>) => void, bind?: any): (arg1: A1) => SilentPromise<T>;
export declare function silentCallback<A1, A2, T>(fn: (arg1: A1, arg2: A2, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, T>(fn: (arg1: A1, arg2: A2, arg3: A3, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9) => SilentPromise<T>;
export declare function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10) => SilentPromise<T>;
export declare function execSilentCallback<T>(fn: RestArgumentFunction, bind: any): SilentPromiseFunction<T>;
export declare function execSilentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;

@@ -8,2 +8,4 @@ "use strict";

exports.promisify = util_1.promisify;
var silent_promise_error_1 = require("./silent-promise-error");
exports.SilentPromiseError = silent_promise_error_1.SilentPromiseError;
const promise_error_1 = __importDefault(require("./promise-error"));

@@ -13,9 +15,9 @@ class SilentPromise extends Promise {

exports.SilentPromise = SilentPromise;
class SilentPromiseError {
constructor(error, message) {
this.error = error;
this.message = message;
}
exports.bind = Symbol("_silent_promise_callback_bind_arg");
function bindTo(thisArg) {
const bindArg = {};
bindArg[exports.bind] = thisArg;
return bindArg;
}
exports.SilentPromiseError = SilentPromiseError;
exports.bindTo = bindTo;
function silentPromise(promise) {

@@ -30,17 +32,42 @@ const p = promise;

exports.silentPromise = silentPromise;
exports.runSilentCallback = runFunctionWithArguments;
function silentCallback(fn, ...args) {
if (args.length === 0) {
return wrapFunction(fn);
// above functions into 1 - wraps fn into a function that returns SilentPromise (SilentPromiseFunction)
function silentCallback(fn, bind) {
const wrapFn = getSilentPromiseFunction(fn, getBindArgument(bind) || bind);
console.log(bind);
Object.setPrototypeOf(wrapFn, Object.getPrototypeOf(fn));
return Object.defineProperties(wrapFn, Object.getOwnPropertyDescriptors(fn));
}
exports.silentCallback = silentCallback;
function execSilentCallback(fn, ...args) {
const bindArgs = getBindArgument(...args);
if (bindArgs) {
return getSilentPromiseFunction(fn, bindArgs);
}
else {
return runFunctionWithArguments(fn, ...args);
return new Promise((resolve) => {
// append callback to intended function call
args.push((error, result) => {
if (error) {
resolve([new promise_error_1.default(error), undefined]);
}
else {
resolve([new promise_error_1.default(), result]);
}
});
fn.call(this, ...args);
});
}
}
exports.silentCallback = silentCallback;
function wrapFunction(fn) {
const wrapFn = (...args) => {
exports.execSilentCallback = execSilentCallback;
function getBindArgument(...args) {
// must be the only argument
if (args.length === 1 && args[0] instanceof Object && args[0][exports.bind]) {
return args[0][exports.bind];
}
}
function getSilentPromiseFunction(fn, bindArg) {
return (...args) => {
return new Promise((resolve) => {
try {
fn.call(this, ...args, (error, result) => {
fn.call(bindArg ? bindArg : this, ...args, (error, result) => {
if (error) {

@@ -59,111 +86,3 @@ resolve([new promise_error_1.default(error), undefined]);

};
Object.setPrototypeOf(wrapFn, Object.getPrototypeOf(fn));
return Object.defineProperties(wrapFn, Object.getOwnPropertyDescriptors(fn));
}
function runFunctionWithArguments(fn, ...args) {
return new Promise((resolve) => {
// append callback to intended function call
args.push((error, result) => {
if (error) {
resolve([new promise_error_1.default(error), undefined]);
}
else {
resolve([new promise_error_1.default(), result]);
}
});
fn.call(this, ...args);
});
}
// function noArgPass(cb: Callback<any>) {
// cb(undefined, true);
// }
//
// function noArgFail(cb: Callback<any>) {
// cb("noArgFail");
// }
//
// function argPass(name: string, cb: Callback<any>) {
// cb(undefined, name);
// }
//
// function argFail(name: string, cb: Callback<any>) {
// cb(`argFail for arg: ${name}`);
// }
//
// function multipleArgsPass(name: string, age: number, location: string, cb: Callback<any>) {
// cb(undefined, `${name}, ${age}, ${location}`);
// }
//
// function multipleArgsFail(name: string, age: number, location: string, cb: Callback<any>) {
// cb(`multipleArgsFail for arg: ${name}, ${age}, ${location}`);
// }
//
// async function testOrdinaryCallbackIndirect() {
// let error, result;
//
// [error, result] = await silentCallback(noArgPass)(); // expect no error
// console.log({ expect: "Indirect noArgPass", error: error.getError(), result: result });
//
// [error, result] = await silentCallback(noArgFail)();
// console.log({ expect: "Indirect noArgFail", error: error.getError(), result: result });
// }
//
// async function testOrdinaryCallbackDirect() {
// let error, result;
//
// [error, result] = await runSilentCallback(noArgPass);
// console.log({ expect: "Direct noArgPass", error: error.getError(), result: result });
//
// [error, result] = await runSilentCallback(noArgFail);
// console.log({ expect: "Direct noArgFail", error: error.getError(), result: result });
// }
//
// async function testArgCallbackIndirect() {
// let error, result;
//
// [error, result] = await silentCallback(argPass)("Wisdom"); // expect no error
// console.log({ expect: "Indirect argPass", error: error.getError(), result: result });
//
// [error, result] = await silentCallback(argFail)("Wisdom");
// console.log({ expect: "Indirect argFail", error: error.getError(), result: result });
// }
//
// async function testArgCallbackDirect() {
// let error, result;
//
// [error, result] = await runSilentCallback(argPass, "Wisdom");
// console.log({ expect: "Direct argPass", error: error.getError(), result: result });
//
// [error, result] = await runSilentCallback(argFail, "Wisdom");
// console.log({ expect: "Direct argFail", error: error.getError(), result: result });
// }
//
// async function testMultipleArgsCallbackIndirect() {
// let error, result;
//
// [error, result] = await silentCallback(multipleArgsPass)("Wisdom", 20, "Port Harcourt"); // expect no error
// console.log({ expect: "Indirect multipleArgsPass", error: error.getError(), result: result });
//
// [error, result] = await silentCallback(multipleArgsFail)("Wisdom", 20, "Port Harcourt");
// console.log({ expect: "Indirect multipleArgsFail", error: error.getError(), result: result });
// }
//
// async function testMultipleArgsCallbackDirect() {
// let error, result;
//
// [error, result] = await runSilentCallback(multipleArgsPass, "Wisdom", 20, "Port Harcourt");
// console.log({ expect: "Direct multipleArgsPass", error: error.getError(), result: result });
//
// [error, result] = await runSilentCallback(multipleArgsFail, "Wisdom", 20, "Port Harcourt");
// console.log({ expect: "Direct multipleArgsFail", error: error.getError(), result: result });
// }
//
// testOrdinaryCallbackIndirect();
// testOrdinaryCallbackDirect();
//
// testArgCallbackIndirect();
// testArgCallbackDirect();
//
// testMultipleArgsCallbackIndirect();
// testMultipleArgsCallbackDirect();
//# sourceMappingURL=index.js.map

@@ -21,10 +21,10 @@ "use strict";

test("silentCallback on successCallbackFunction", () => __awaiter(this, void 0, void 0, function* () {
yield expect(src_1.runSilentCallback(successCallbackFunction, 1, 2, 3)).resolves.toBeDefined();
yield expect(src_1.execSilentCallback(successCallbackFunction, 1, 2, 3)).resolves.toBeDefined();
}));
test("silentPromise on errorCallbackFunction", () => __awaiter(this, void 0, void 0, function* () {
yield expect(src_1.runSilentCallback(errorCallbackFunction)).resolves.toBeDefined();
yield expect(src_1.execSilentCallback(errorCallbackFunction)).resolves.toBeDefined();
}));
});
describe("await silentCallback on successCallbackFunction", () => {
beforeAll(() => sPromise = src_1.runSilentCallback(successCallbackFunction, 1, 2, 3));
beforeAll(() => sPromise = src_1.execSilentCallback(successCallbackFunction, 1, 2, 3));
test('error.getError() should be undefined', () => __awaiter(this, void 0, void 0, function* () {

@@ -54,3 +54,3 @@ expect.assertions(1);

describe("await silentPromise on errorCallbackFunction", () => {
beforeAll(() => sPromise = src_1.runSilentCallback(errorCallbackFunction));
beforeAll(() => sPromise = src_1.execSilentCallback(errorCallbackFunction));
test('result should be undefined', () => __awaiter(this, void 0, void 0, function* () {

@@ -57,0 +57,0 @@ expect.assertions(1);

@@ -77,2 +77,26 @@ "use strict";

});
describe("silentCallback on successCallbackFunction with and without bind", () => {
function fn(n1, n2, cb) {
this.test.call(n1 + n2);
cb(undefined, n1 + n2);
}
test("should attach bind argument as this", () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const thisArg = {
test: {
call: jest.fn()
}
};
yield src_1.silentCallback(fn, thisArg)(1, 2);
expect(thisArg.test.call).toHaveBeenLastCalledWith(3);
}));
test("successCallbackFunction should throw error because this is undefined", () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const [error] = yield src_1.silentCallback(fn)(1, 2);
const throwError = () => {
throw error.getError();
};
expect(throwError).toThrow(TypeError);
}));
});
//# sourceMappingURL=silent-callback.test.js.map
{
"name": "silent-promise",
"version": "0.0.0",
"version": "0.0.1",
"description": "Make a promise fail silently. For use with async/await for easy error handling without try-catch",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -8,7 +8,13 @@ export { promisify } from "util";

export const bind = Symbol("_silent_promise_callback_bind_arg");
export function bindTo(thisArg: any): any {
const bindArg: any = {};
bindArg[bind] = thisArg;
return bindArg;
}
export type Callback<T> = (error: any | undefined, result?: T) => void;
type Args = any[];
type RestArgumentFunction = (...args: Args) => void;
type SilentPromiseFunction<T> = (...args: Args) => SilentPromise<T>;
type SilentCallbackResult<T> = SilentPromiseFunction<T> | SilentPromise<T>;
export type Args = any[];
export type RestArgumentFunction = (...args: Args) => void;
export type SilentPromiseFunction<T> = (...args: Args) => SilentPromise<T>;

@@ -24,45 +30,67 @@ export function silentPromise<T>(promise: Promise<T>): SilentPromise<T> {

export const runSilentCallback = runFunctionWithArguments;
export function silentCallback<T>(fn: (callback: Callback<T>) => void): () => SilentPromise<T>;
export function silentCallback<T>(fn: (callback: Callback<T>) => void, bind?: any): () => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, T>(fn: (arg1: A1, callback: Callback<T>) => void): (arg1: A1) => SilentPromise<T>;
export function silentCallback<A1, T>(fn: (arg1: A1, callback: Callback<T>) => void, bind?: any): (arg1: A1) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, T>(fn: (arg1: A1, arg2: A2, callback: Callback<T>) => void): (arg1: A1, arg2: A2) => SilentPromise<T>;
export function silentCallback<A1, A2, T>(fn: (arg1: A1, arg2: A2, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, T>(fn: (arg1: A1, arg2: A2, arg3: A3, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, T>(fn: (arg1: A1, arg2: A2, arg3: A3, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, A4, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, A4, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, A4, A5, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, A4, A5, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, A4, A5, A6, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, A4, A5, A6, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9) => SilentPromise<T>;
// tslint:disable-next-line:max-line-length
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10, callback: Callback<T>) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10) => SilentPromise<T>;
export function silentCallback<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, T>(fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10, callback: Callback<T>) => void, bind?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, arg6: A6, arg7: A7, arg8: A8, arg9: A9, arg10: A10) => SilentPromise<T>;
// above functions into 1 - wraps fn into a function that returns SilentPromise (SilentPromiseFunction)
export function silentCallback<T>(fn: RestArgumentFunction): SilentPromiseFunction<T>;
export function silentCallback<T>(fn: RestArgumentFunction, bind?: any): SilentPromiseFunction<T> {
const wrapFn = getSilentPromiseFunction(fn, getBindArgument(bind) || bind);
console.log(bind);
Object.setPrototypeOf(wrapFn, Object.getPrototypeOf(fn));
return Object.defineProperties(wrapFn, Object.getOwnPropertyDescriptors(fn));
}
// additional function that runs fn and returns SilentPromise straight away
export function silentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;
export function execSilentCallback<T>(fn: RestArgumentFunction, bind: any): SilentPromiseFunction<T>;
export function execSilentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T>;
export function execSilentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T> | SilentPromiseFunction<T> {
const bindArgs = getBindArgument(...args);
if (bindArgs) {
return getSilentPromiseFunction<T>(fn, bindArgs);
}
else {
return new Promise((resolve) => {
// append callback to intended function call
args.push((error: any, result: T) => {
if (error) {
resolve([ new PromiseError(error), undefined ]);
} else {
resolve([ new PromiseError(), result ]);
}
});
export function silentCallback<T>(fn: RestArgumentFunction, ...args: Args): SilentCallbackResult<T> {
if (args.length === 0) {
return wrapFunction(fn);
} else {
return runFunctionWithArguments(fn, ...args);
fn.call(this, ...args);
});
}
}
function wrapFunction<T>(fn: RestArgumentFunction): SilentPromiseFunction<T> {
const wrapFn = (...args: Args): SilentPromise<T> => {
function getBindArgument(...args: Args) {
// must be the only argument
if (args.length === 1 && args[0] instanceof Object && args[0][bind]) {
return args[0][bind];
}
}
function getSilentPromiseFunction<T>(fn: RestArgumentFunction, bindArg?: any) {
return (...args: Args): SilentPromise<T> => {
return new Promise((resolve) => {
try {
fn.call(this, ...args, (error: any | undefined, result: T) => {
fn.call(bindArg ? bindArg : this, ...args, (error: any | undefined, result: T) => {
if (error) {

@@ -79,20 +107,2 @@ resolve([new PromiseError(error), undefined]);

};
Object.setPrototypeOf(wrapFn, Object.getPrototypeOf(fn));
return Object.defineProperties(wrapFn, Object.getOwnPropertyDescriptors(fn));
}
function runFunctionWithArguments<T>(fn: RestArgumentFunction, ...args: Args): SilentPromise<T> {
return new Promise((resolve) => {
// append callback to intended function call
args.push((error: any, result: T) => {
if (error) {
resolve([ new PromiseError(error), undefined ]);
} else {
resolve([ new PromiseError(), result ]);
}
});
fn.call(this, ...args);
});
}

@@ -82,1 +82,29 @@ import { Callback, silentCallback, SilentPromise, SilentPromiseError } from "../src";

});
describe("silentCallback on successCallbackFunction with and without bind", () => {
function fn(n1: number, n2: number, cb: Callback<number>) {
this.test.call(n1 + n2);
cb(undefined, n1 + n2);
}
test("should attach bind argument as this", async () => {
expect.assertions(1);
const thisArg = {
test: {
call: jest.fn()
}
};
await silentCallback(fn, thisArg)(1, 2);
expect(thisArg.test.call).toHaveBeenLastCalledWith(3);
});
test("successCallbackFunction should throw error because this is undefined", async () => {
expect.assertions(1);
const [error] = await silentCallback(fn)(1, 2);
const throwError = () => {
throw error.getError();
};
expect(throwError).toThrow(TypeError);
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc