Skip to content

Commit

Permalink
A small code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream committed Apr 13, 2020
1 parent ae290c5 commit 28026eb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = [
{
path: "use-suspender.js",
limit: "556 B"
limit: "564 B"
}
]
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ npm i use-suspender

Creates a new useSuspender for given function.

- **{Function}** suspender – a function that will be used for each useSuspender call.
- **{Function}** fn – a function that will be used for each useSuspender call.
- **{any}** ctx – thisArg that will be used for each useSuspender call.

### `useSuspender([...args]) -> {any}`
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ test("Throws an error when createSuspender called witout an argument", t => {
const err = t.throws(() => createSuspender())

t.true(err instanceof TypeError)
t.is(err.message, "Suspender expected to be a function.")
t.is(err.message, "First argument expected to be a function.")
})

test("Throws an error thrown by suspender", async t => {
Expand Down
15 changes: 7 additions & 8 deletions use-suspender.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ function getPromise(fn, args, ctx) {
*
* @template T
*
* @param {(...args: any[]) => T} suspender A function to make a useSuspender hook with
* @param {(...args: any[]) => T} fn A function to create a useSuspender hook with
* @param {any} [ctx = undefined] thisArg value
*
* @return {(...args: any[]) => T} useSuspender
*
* @api public
*/
function createSuspender(suspender, ctx) {
if (typeof suspender !== "function") {
throw new TypeError("Suspender expected to be a function.")
function createSuspender(fn, ctx) {
if (typeof fn !== "function") {
throw new TypeError("First argument expected to be a function.")
}

let operation = {...initialOperationState}
Expand All @@ -84,14 +84,13 @@ function createSuspender(suspender, ctx) {
* Calls a suspender function and sets its Promise on the operation
* Takes the same arguments as getPromise function.
*
* @param {(...args: any[]) => any} fn
* @param {any[]} args
*
* @return {Promise<void>}
*
* @api private
*/
function call(fn, args) {
function call(args) {
operation.suspender = getPromise(fn, args, ctx)
.then(result => {
operation.result = result
Expand Down Expand Up @@ -140,7 +139,7 @@ function createSuspender(suspender, ctx) {
return result
}

throw call(suspender, args)
throw call(args)
}

/**
Expand All @@ -153,7 +152,7 @@ function createSuspender(suspender, ctx) {
* @api public
*/
useSuspender.callEarly = function callEarly(...args) {
call(suspender, args)
call(args)
}

// For those who want to use object destructing on createSuspender result.
Expand Down

0 comments on commit 28026eb

Please sign in to comment.