From c6318cb8dbce7ad9ddd2a86ec6341c36252f29d8 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Tue, 24 Jul 2018 01:33:17 -0400 Subject: [PATCH] Add callbackToPromise to util.ts --- src/ts/util.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/ts/util.ts diff --git a/src/ts/util.ts b/src/ts/util.ts new file mode 100644 index 0000000..0e88ddf --- /dev/null +++ b/src/ts/util.ts @@ -0,0 +1,14 @@ +/** + * callbackToPromise converts a callback based function to a promise based function. + * The function's only argument must be a function. + * + * @param {(Function) => void} func + * @returns {PromiseLike} + */ +export function callbackToPromise(func: (Function) => void): PromiseLike { + return new Promise((resolve) => { + func((...callbackArgs) => { + resolve(callbackArgs); + }); + }); +}