Skip to content

Commit

Permalink
pass context to callback
Browse files Browse the repository at this point in the history
  • Loading branch information
MZanggl committed Jan 4, 2020
1 parent 2d6079c commit 0123a4d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function promistate<T>(action: (...args: CallbackArgs) => Promise<T>, options: P
this.isEmpty = false
this.error = null

return action(...args)
return action.apply(this, args)
.then((result: T) => {
this.isEmpty = isEmpty<T>(result)
this.value = result
Expand Down
12 changes: 11 additions & 1 deletion test/promistate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,14 @@ test('can pass custom isEmpty check', async (assert) => {
})

assert.isTrue(state.isEmpty)
})
})

test('can access state in load function', async (assert) => {
const state = promistate(async function() {
return this.value + 1
}, { defaultValue: 1 })

await state.load()

assert.equal(state.value, 2)
})

0 comments on commit 0123a4d

Please sign in to comment.