Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type Callback<T> = <E extends Error>(err: E, value: T) => never;

interface DeAsync {
<R>(fn: (callback: Callback<R>) => void): () => R;
<T1, R>(fn: (arg1: T1, callback: Callback<R>) => void): (arg1: T1) => R;
<T1, T2, R>(fn: (arg1: T1, arg2: T2, callback: Callback<R>) => void): (arg1: T1, arg2: T2) => R;
<T1, T2, T3, R>(fn: (arg1: T1, arg2: T2, arg3: T3, callback: Callback<R>) => void): (arg1: T1, arg2: T2, arg3: T3) => R;
<T1, T2, T3, T4, R>(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: Callback<R>) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R;
<T1, T2, T3, T4, T5, R>(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: Callback<R>) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => R;
<T1, T2, T3, T4, T5, T6, R>(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, callback: Callback<R>) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => R;
(fn: Function): Function

sleep(timeout: number): void;

runLoopOnce(): void;

loopWhile(pred: () => boolean): void;

await<T>(promise: Promise<T>): T;
}

declare const instance: DeAsync;

export = instance;
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,19 @@
}
};

module.exports.await = function (promise) {
if (promise === null) { return; }
if (typeof promise.then !== "function") { return; }
var resolved = false, rejected = false,
result, error;
promise.then(
function (value) { resolved = true; result = value; return result; },
function (reason) { rejected = true; error = reason; return reason; }
)
module.exports.loopWhile(function () { return !resolved && !rejected; })
if (rejected) {
throw error;
}
return result;
Comment on lines +79 to +89
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not work with async terser.minify()
the promise is never resolved or rejected = deadloop

return result and return reason in the callback fns is pointless

}
}());
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.13",
"description": "Turns async function into sync via JavaScript wrapper of Node event loop",
"main": "index.js",
"types": "index.d.ts",
"author": "Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com>",
"contributors": [
"Fred Wen <wenfred@gmail.com> (https://github.com/abbr)"
Expand Down