| 11 | } |
| 12 | |
| 13 | function enableYield(fn, message, runnable) { |
| 14 | if ( |
| 15 | !runnable || |
| 16 | !runnable.constructor || |
| 17 | runnable.constructor.name !== 'GeneratorFunction' |
| 18 | ) { |
| 19 | return fn(message, runnable); |
| 20 | } |
| 21 | return fn(message, (done) => { |
| 22 | const iterator = runnable(); |
| 23 | function step(method, result) { |
| 24 | let state; |
| 25 | try { |
| 26 | state = iterator[method](result); |
| 27 | } catch (e) { |
| 28 | // catch any assertion errors and pass to `done` |
| 29 | // otherwise the messages are swallowed |
| 30 | return done(e); |
| 31 | } |
| 32 | if (state.done) { |
| 33 | Promise.resolve(state.value).then(() => done(), done); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | Promise.resolve(state.value).then(_next, _throw); |
| 38 | } |
| 39 | |
| 40 | function _next(value) { |
| 41 | step('next', value); |
| 42 | } |
| 43 | |
| 44 | function _throw(error) { |
| 45 | step('throw', error); |
| 46 | } |
| 47 | |
| 48 | _next(); |
| 49 | }); |
| 50 | } |