Generator functions are at the core of Koa.js, so let's dive right into dissecting this beast. Generators allow adept JavaScript users to implement functions in completely new ways. Koa.js makes use of the new syntax to write code in a synchronous-looking fashion while maintaining the performance benefits of an asynchronous flow.
The following defines a simple generator function in src/helloGenerator.js
(note the asterisk syntax):
module.exports = function *() { return 'hello generator'; };
To use Mocha with Koa.js:
You will need to include
co-mocha
to add generator support, requiring once at the first line of each test file is the safe way to do it. Now you can pass generator functions to Mocha'sit
function as follows:require('co-mocha'); var expect = require('chai').expect; var helloGenerator = require('../src/helloGenerator'); describe('Hello Generator', function() { it('should yield to the function and return hello', function *() { var ans = yield helloGenerator...