Skip to content

Commit

Permalink
Anything can now be yielded from flows
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Mar 12, 2018
1 parent fe8131c commit 5ab68b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/api/flow.ts
Expand Up @@ -135,8 +135,8 @@ export function createFlowGenerator(name: string, generator: Function) {
} catch (e) {
return reject(e)
}

next(ret)
return null
}

function onRejected(err: any) {
Expand All @@ -155,10 +155,7 @@ export function createFlowGenerator(name: string, generator: Function) {

function next(ret: any) {
if (ret.done) return resolve(ret.value)
// TODO: support more type of values? See https://github.com/tj/co/blob/249bbdc72da24ae44076afd716349d2089b31c4c/index.js#L100
if (!ret.value || typeof ret.value.then !== "function")
return fail("Only promises can be yielded to asyncAction, got: " + ret)
pendingPromise = ret.value
pendingPromise = Promise.resolve(ret.value) as any
return pendingPromise!.then(onFulfilled, onRejected)
}

Expand Down
11 changes: 11 additions & 0 deletions test/base/flow.js
Expand Up @@ -317,3 +317,14 @@ test("flows can be cancelled - 5 - flows cancel recursively", done => {
)
p.cancel()
})

test("flows yield anything", async () => {
let steps = 0
const start = flow(function*() {
const x = yield 2
return x
})

const res = await start()
expect(res).toBe(2)
})

0 comments on commit 5ab68b1

Please sign in to comment.