Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var Bluebird = require('bluebird');
- // I do not want to wrap this in a coroutine.
- function *ping(val) {
- console.log('Ping?');
- yield Bluebird.delay(500);
- pong(val + 1);
- }
- // I do not want to wrap this in a coroutine.
- function *pong(val) {
- console.log('Pong?');
- yield Bluebird.delay(500);
- pong(val + 1);
- }
- // Here I want to magically convert a Generator function to a Promise.
- Bluebird.coroutine.addYieldHandler(function (gen) {
- if (gen && gen.next && gen.next.call) {
- return Promise.try(function cont(a) {
- var n = gen.next(a);
- if (n.done) {
- return n.value;
- }
- if (!n.value.then) {
- return cont(n.value);
- }
- return n.value.catch(gen.throw.bind(gen)).then(cont);
- });
- }
- });
- // This is the main start function.
- Bluebird.coroutine(function *() {
- yield ping(10);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement