Skip to content

Commit 34b0b79

Browse files
Merge pull request cerebral#76 from cerebral/doPromise
feat(overmind): allow do operator to return promise
2 parents c0b0262 + 2e4c531 commit 34b0b79

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

packages/node_modules/overmind/src/Action.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,17 @@ export default class ActionClass<
125125
) as any
126126
}
127127
do: (
128-
cb: (effects: Effects, value: Value) => void
128+
cb: (effects: Effects, value: Value) => void | Promise<any>
129129
) => [InitialValue] extends [void]
130130
? INoValueAction<State, Effects, InitialValue, Value>
131131
: IValueAction<State, Effects, InitialValue, Value> = (cb) => {
132132
const operator = (effects, value) => {
133-
cb(effects, value)
133+
const result = cb(effects, value)
134+
135+
if (result instanceof Promise) {
136+
return result.then(() => value)
137+
}
138+
134139
return value
135140
}
136141

packages/node_modules/overmind/src/index.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ describe('OPERATORS', () => {
164164
const doThis: Action<string> = (action) =>
165165
action().do(({ foo }) => {
166166
expect(foo.bar()).toBe('baz')
167+
return Promise.resolve('bihihi')
167168
})
168169
const config = {
169170
state: {
@@ -190,7 +191,9 @@ describe('OPERATORS', () => {
190191
type Action<Input = void, Output = any> = TAction<Input, Output, Config>
191192

192193
const app = new App(config)
193-
expect(app.actions.doThis('foo')).toBe('foo')
194+
return app.actions
195+
.doThis('foo')
196+
.then((result) => expect(result).toBe('foo'))
194197
})
195198
test('map', () => {
196199
expect.assertions(1)

packages/overmind-website/api/action.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ h(Example, { name: "api/action_do" })
3333

3434
Typically used to fire off an effect without caring about its returned result, if any.
3535

36-
Only argument is a function that receives the **effects** registered in the application as the first argument, and the current **value** of the action as the second argument. Any returned value will be ignored. The current value of the action will be passed to the next operator.
36+
Only argument is a function that receives the **effects** registered in the application as the first argument, and the current **value** of the action as the second argument. Any returned value will be ignored, though you can return a promise to indicate that the do operator needs to be resolved. The current value of the action will be passed to the next operator.
3737

3838
## filter
3939
```marksy

0 commit comments

Comments
 (0)