@@ -118,12 +118,14 @@ export function createFlowGenerator(name: string, generator: Function) {
118118 const runId = ++ generatorId
119119 const gen = action ( `${ name } - runid: ${ runId } - init` , generator ) . apply ( ctx , args )
120120 let rejector : ( error : any ) => void
121+ let pendingPromise : CancellablePromise < any > | undefined = undefined
121122
122123 const res = new Promise ( function ( resolve , reject ) {
123124 let stepId = 0
124125 rejector = reject
125126
126127 function onFulfilled ( res : any ) {
128+ pendingPromise = undefined
127129 let ret
128130 try {
129131 ret = action ( `${ name } - runid: ${ runId } - yield ${ stepId ++ } ` , gen . next ) . call (
@@ -138,6 +140,7 @@ export function createFlowGenerator(name: string, generator: Function) {
138140 }
139141
140142 function onRejected ( err : any ) {
143+ pendingPromise = undefined
141144 let ret
142145 try {
143146 ret = action ( `${ name } - runid: ${ runId } - yield ${ stepId ++ } ` , gen . throw ) . call (
@@ -155,14 +158,17 @@ export function createFlowGenerator(name: string, generator: Function) {
155158 // TODO: support more type of values? See https://github.com/tj/co/blob/249bbdc72da24ae44076afd716349d2089b31c4c/index.js#L100
156159 if ( ! ret . value || typeof ret . value . then !== "function" )
157160 return fail ( "Only promises can be yielded to asyncAction, got: " + ret )
158- return ret . value . then ( onFulfilled , onRejected )
161+ pendingPromise = ret . value
162+ return pendingPromise ! . then ( onFulfilled , onRejected )
159163 }
160164
161165 onFulfilled ( undefined ) // kick off the process
162166 } ) as any
163167
164168 res . cancel = action ( `${ name } - runid: ${ runId } - cancel` , function ( ) {
165169 try {
170+ if ( pendingPromise && typeof pendingPromise . cancel === "function" )
171+ pendingPromise . cancel ( )
166172 gen . return ( )
167173 rejector ( new Error ( "FLOW_CANCELLED" ) )
168174 } catch ( e ) {
0 commit comments