-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from rzane/no-async
Trim bundle size by avoiding async/await
- Loading branch information
Showing
6 changed files
with
86 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* If the value returned by the function is not a promise, convert it. | ||
* Make sure to catch errors that are thrown syncronously. | ||
*/ | ||
export function toPromise<T>(fn: () => T | PromiseLike<T>): Promise<T> { | ||
try { | ||
return Promise.resolve(fn()); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { toPromise } from "../../src/utilities/toPromise"; | ||
|
||
describe("toPromise", () => { | ||
test("converts sync success", async () => { | ||
const promise = toPromise(() => 1); | ||
expect(promise).toBeInstanceOf(Promise); | ||
await expect(promise).resolves.toEqual(1); | ||
}); | ||
|
||
test("converts sync error", async () => { | ||
const promise = toPromise(() => { | ||
throw new Error("boom"); | ||
}); | ||
|
||
expect(promise).toBeInstanceOf(Promise); | ||
await expect(promise).rejects.toThrow(/boom/); | ||
}); | ||
|
||
test("converts async success", async () => { | ||
const promise = toPromise(() => Promise.resolve(1)); | ||
|
||
expect(promise).toBeInstanceOf(Promise); | ||
await expect(promise).resolves.toEqual(1); | ||
}); | ||
|
||
test("converts async error", async () => { | ||
const promise = toPromise(() => Promise.reject(new Error("boom"))); | ||
|
||
expect(promise).toBeInstanceOf(Promise); | ||
await expect(promise).rejects.toThrow(/boom/); | ||
}); | ||
}); |