From bff61402061583e33e1ad2985dd6ad68244213bb Mon Sep 17 00:00:00 2001 From: Septs Date: Sun, 5 Aug 2018 14:16:31 +0800 Subject: [PATCH 1/4] Add the correct type implementation. --- index.d.ts | 21 +++++++++++++++++++++ package.json | 1 + 2 files changed, 22 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..8c31f0b --- /dev/null +++ b/index.d.ts @@ -0,0 +1,21 @@ +type Callback = (err: E, value: T) => never; + +interface DeAsync { + (fn: (callback: Callback) => void): () => R; + (fn: (arg1: T1, callback: Callback) => void): (arg1: T1) => R; + (fn: (arg1: T1, arg2: T2, callback: Callback) => void): (arg1: T1, arg2: T2) => R; + (fn: (arg1: T1, arg2: T2, arg3: T3, callback: Callback) => void): (arg1: T1, arg2: T2, arg3: T3) => R; + (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: Callback) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R; + (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: Callback) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => R; + (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, callback: Callback) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => R; + + sleep(timeout: number): void; + + runLoopOnce(): void; + + loopWhile(pred: () => boolean): void; +} + +declare const instance: DeAsync; + +export = instance; diff --git a/package.json b/package.json index 93326db..dc06ef0 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.13", "description": "Turns async function into sync via JavaScript wrapper of Node event loop", "main": "index.js", + "typing": "index.d.ts", "author": "Vladimir Kurchatkin ", "contributors": [ "Fred Wen (https://github.com/abbr)" From cc505df9e7894fa249ba3d371fc5bc6f71bd05f1 Mon Sep 17 00:00:00 2001 From: Septs Date: Sun, 5 Aug 2018 14:35:40 +0800 Subject: [PATCH 2/4] Add fallback --- index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.d.ts b/index.d.ts index 8c31f0b..ad6abee 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,6 +8,7 @@ interface DeAsync { (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: Callback) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: Callback) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => R; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, callback: Callback) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => R; + (fn: Function): Function sleep(timeout: number): void; From 1d9b2d9e6a43a1b377a48ef1885e5d34d0fa3405 Mon Sep 17 00:00:00 2001 From: Septs Date: Sun, 5 Aug 2018 15:10:13 +0800 Subject: [PATCH 3/4] Add Promise supports. --- index.d.ts | 2 ++ index.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/index.d.ts b/index.d.ts index ad6abee..584d787 100644 --- a/index.d.ts +++ b/index.d.ts @@ -15,6 +15,8 @@ interface DeAsync { runLoopOnce(): void; loopWhile(pred: () => boolean): void; + + await(promise: Promise): T; } declare const instance: DeAsync; diff --git a/index.js b/index.js index 28acb68..e4c084c 100644 --- a/index.js +++ b/index.js @@ -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; + } }()); From 4b33e593d85bdf00e7a332feea71e58de757fec6 Mon Sep 17 00:00:00 2001 From: Septs Date: Mon, 6 Aug 2018 16:07:44 +0800 Subject: [PATCH 4/4] Fix typo error, see https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dc06ef0..6543df4 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.1.13", "description": "Turns async function into sync via JavaScript wrapper of Node event loop", "main": "index.js", - "typing": "index.d.ts", + "types": "index.d.ts", "author": "Vladimir Kurchatkin ", "contributors": [ "Fred Wen (https://github.com/abbr)"