Skip to content

Instantly share code, notes, and snippets.

@diyan
Last active September 25, 2016 08:55
Show Gist options
  • Save diyan/23849e2244fb07600f17 to your computer and use it in GitHub Desktop.
Save diyan/23849e2244fb07600f17 to your computer and use it in GitHub Desktop.
Evaluate how different ES6 generator runners (Q, Bluebird and Co) behaves with buggy code

Evaluate how different ES6 generator runners (Q, Bluebird and Co) behaves with buggy code

  • NodeJS v5.6.0
  • q@1.4.1
  • bluebird@3.3.1
  • co@4.6.0
var Q = require('q');
var bluebird = require('bluebird');
var co = require('co');

// Offtopic. Generator fn as a solution to lazy collection as iterables
function* items() {
  yield 1;
  yield 2;
  yield 3;
}

// Generator fn as a solution to async function composition
function* sum() {
  var x = yield Promise.resolve(1);
  var y = yield Promise.resolve(2);
  var z = yield Promise.resolve(3);
  return x + y + z;
}

function* f() {
  var x = yield* sum();
  console.log('x ==', x);
}

Q.spawn(f);
bluebird.coroutine(f)();
co(f);

Buggy code #1

-function* f() {
+function f() {

bluebird, q

  var x = yield* sum();
          ^
ReferenceError: yield is not defined

co

Runs but nothing is printed

Buggy code #2

-function* sum() {
+function sum() {

bluebird, co, q

  var x = yield Promise.resolve(1);
                ^^^^^^^
SyntaxError: Unexpected identifier

Buggy code #3

- var x = yield* sum();
+ var x = yield sum();

bluebird

Unhandled rejection TypeError: A value [object Generator] was yielded that could not be treated as a promise

 See http://goo.gl/MqrFmX

co

TODO Check why buggy code works - by accident or it's intentional behavior 
x == 6

q

Invalid result: x is {} but 6 is expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment