From 0123a4dbc6359ebc8a93468216c8ddbc6bfe8de9 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 4 Jan 2020 22:21:58 +0900 Subject: [PATCH] pass context to callback --- src/index.ts | 2 +- test/promistate.spec.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index ba196bb..311fd7f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,7 +41,7 @@ function promistate(action: (...args: CallbackArgs) => Promise, options: P this.isEmpty = false this.error = null - return action(...args) + return action.apply(this, args) .then((result: T) => { this.isEmpty = isEmpty(result) this.value = result diff --git a/test/promistate.spec.js b/test/promistate.spec.js index 812e669..fc1b7f7 100644 --- a/test/promistate.spec.js +++ b/test/promistate.spec.js @@ -152,4 +152,14 @@ test('can pass custom isEmpty check', async (assert) => { }) assert.isTrue(state.isEmpty) -}) \ No newline at end of file +}) + +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) +})