-
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.
- Loading branch information
1 parent
826ff13
commit 1988562
Showing
5 changed files
with
85 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
pnpm-lock.yaml |
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 |
---|---|---|
@@ -1,2 +1,24 @@ | ||
# quick-resolve | ||
A Synchronous Promise.resolve. Preserve Sync/Async. Only Go Async If Necessary. | ||
|
||
# install | ||
```bash | ||
npm install quick-resolve | ||
``` | ||
|
||
# usage | ||
```js | ||
import quickResolve from "quick-resolve"; | ||
|
||
let resolved = false; | ||
quickResolve(1).then(num => (resolved = true)); | ||
// resolved is true | ||
|
||
let resolved = false; | ||
quickResolve(fetch(url)).then(response => (resolved = true)); | ||
// resolved is false | ||
|
||
let resolved = false; | ||
await quickResolve(fetch(url)).then(response => (resolved = true)); | ||
// resolved is true | ||
``` |
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,28 @@ | ||
{ | ||
"name": "quick-resolve", | ||
"version": "0.0.0", | ||
"description": "A Synchronous Promise.resolve. Preserve Sync/Async. Only Go Async If Necessary.", | ||
"main": "quick-resolve.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/DanielJDufour/quick-resolve.git" | ||
}, | ||
"keywords": [ | ||
"async", | ||
"promise", | ||
"resolve", | ||
"sync" | ||
], | ||
"author": "Daniel J. Dufour", | ||
"license": "CC0-1.0", | ||
"bugs": { | ||
"url": "https://github.com/DanielJDufour/quick-resolve/issues" | ||
}, | ||
"homepage": "https://github.com/DanielJDufour/quick-resolve#readme", | ||
"devDependencies": { | ||
"flug": "^2.1.0" | ||
} | ||
} |
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,8 @@ | ||
module.exports = function resolve(it) { | ||
if (typeof it === "object" && typeof it.then === "function") { | ||
// it appears to be a promise | ||
return it; | ||
} else { | ||
return { then: func => func(it) }; | ||
} | ||
}; |
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,25 @@ | ||
const test = require("flug"); | ||
const resolve = require("./quick-resolve.js"); | ||
|
||
test("sync number", ({ eq }) => { | ||
let checked = false; | ||
resolve(1).then(value => { | ||
eq(value, 1); | ||
checked = true; | ||
}); | ||
eq(checked, true); | ||
}); | ||
|
||
test("async number", async ({ eq }) => { | ||
let checked = false; | ||
await resolve(Promise.resolve(1)).then(value => { | ||
eq(value, 1); | ||
checked = true; | ||
}); | ||
eq(checked, true); | ||
}); | ||
|
||
test("sync number with await", async ({ eq }) => { | ||
const value = await resolve(1); | ||
eq(value, 1); | ||
}); |