From 5ab68b1a72f1a33ff1f601f42a5a9d8d542b8fa5 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Mon, 12 Mar 2018 12:41:48 +0100 Subject: [PATCH] Anything can now be yielded from flows --- src/api/flow.ts | 7 ++----- test/base/flow.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/api/flow.ts b/src/api/flow.ts index cb6fda0ef..2809fe01b 100644 --- a/src/api/flow.ts +++ b/src/api/flow.ts @@ -135,8 +135,8 @@ export function createFlowGenerator(name: string, generator: Function) { } catch (e) { return reject(e) } + next(ret) - return null } function onRejected(err: any) { @@ -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) } diff --git a/test/base/flow.js b/test/base/flow.js index 9e0310265..86cd823a2 100644 --- a/test/base/flow.js +++ b/test/base/flow.js @@ -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) +})