Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ES2015 let, const, for of #249

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_js:
- "0.12"
- "iojs"
- "4.0"
language: node_js
script: "npm run-script test-travis"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
4.7.0 / 2015-07-09
==================

* Drop support for node < 4.0, use ES2016 syntax


4.6.0 / 2015-07-09
==================

Expand Down
36 changes: 13 additions & 23 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

```js
co(function* () {
var result = yield Promise.resolve(true);
let result = yield Promise.resolve(true);
return result;
}).then(function (value) {
console.log(value);
Expand All @@ -32,7 +32,7 @@ co(function* () {
you now use `co.wrap(fn*)`.

```js
var fn = co.wrap(function* (val) {
let fn = co.wrap(function* (val) {
return yield Promise.resolve(val);
});

Expand All @@ -43,17 +43,7 @@ fn(true).then(function (val) {

## Platform Compatibility

`co@4+` requires a `Promise` implementation.
For versions of node `< 0.11` and for many older browsers,
you should/must include your own `Promise` polyfill.

When using node 0.11.x or greater, you must use the `--harmony-generators`
flag or just `--harmony` to get access to generators.

When using node 0.10.x and lower or browsers without generator support,
you must use [gnode](https://github.com/TooTallNate/gnode) and/or [regenerator](http://facebook.github.io/regenerator/).

io.js is supported out of the box, you can use `co` without flags or polyfills.
`co` require a node version higher than 4.0 for Generator, Promise and other ES2015 feature support.

## Installation

Expand All @@ -72,19 +62,19 @@ View the [wiki](https://github.com/visionmedia/co/wiki) for more libraries.
## Examples

```js
var co = require('co');
let co = require('co');

co(function *(){
// yield any promise
var result = yield Promise.resolve(true);
let result = yield Promise.resolve(true);
}).catch(onerror);

co(function *(){
// resolve multiple promises in parallel
var a = Promise.resolve(1);
var b = Promise.resolve(2);
var c = Promise.resolve(3);
var res = yield [a, b, c];
let a = Promise.resolve(1);
let b = Promise.resolve(2);
let c = Promise.resolve(3);
let res = yield [a, b, c];
console.log(res);
// => [1, 2, 3]
}).catch(onerror);
Expand Down Expand Up @@ -136,7 +126,7 @@ be removed in future versions of `co`.

```js
co(function* () {
var res = yield [
let res = yield [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
Expand All @@ -151,7 +141,7 @@ Just like arrays, objects resolve all `yieldable`s in parallel.

```js
co(function* () {
var res = yield {
let res = yield {
1: Promise.resolve(1),
2: Promise.resolve(2),
};
Expand Down Expand Up @@ -182,12 +172,12 @@ co(function* () {
});
```

### var fn = co.wrap(fn*)
### let fn = co.wrap(fn*)

Convert a generator into a regular function that returns a `Promise`.

```js
var fn = co.wrap(function* (val) {
let fn = co.wrap(function* (val) {
return yield Promise.resolve(val);
});

Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "co",
"version": "4.4.0",
"version": "4.7.0",
"description": "generator async control flow goodness",
"keywords": [
"async",
Expand Down
32 changes: 15 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

'use strict';
/**
* slice() reference.
*/

var slice = Array.prototype.slice;
const slice = Array.prototype.slice;

/**
* Expose `co`.
Expand Down Expand Up @@ -41,8 +41,8 @@ co.wrap = function (fn) {
*/

function co(gen) {
var ctx = this;
var args = slice.call(arguments, 1)
const ctx = this;
const args = slice.call(arguments, 1);

// we wrap everything in a promise to avoid promise chaining,
// which leads to memory leak errors.
Expand All @@ -60,7 +60,7 @@ function co(gen) {
*/

function onFulfilled(res) {
var ret;
let ret;
try {
ret = gen.next(res);
} catch (e) {
Expand All @@ -76,7 +76,7 @@ function co(gen) {
*/

function onRejected(err) {
var ret;
let ret;
try {
ret = gen.throw(err);
} catch (e) {
Expand All @@ -96,10 +96,9 @@ function co(gen) {

function next(ret) {
if (ret.done) return resolve(ret.value);
var value = toPromise.call(ctx, ret.value);
let value = toPromise.call(ctx, ret.value);
if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
+ 'but the following object was passed: "' + String(ret.value) + '"'));
return onRejected(new TypeError(`You may only yield a function, promise, generator, array, or object, but the following object was passed: "${String(ret.value)}"`));
}
});
}
Expand Down Expand Up @@ -131,7 +130,7 @@ function toPromise(obj) {
*/

function thunkToPromise(fn) {
var ctx = this;
const ctx = this;
return new Promise(function (resolve, reject) {
fn.call(ctx, function (err, res) {
if (err) return reject(err);
Expand Down Expand Up @@ -164,12 +163,11 @@ function arrayToPromise(obj) {
*/

function objectToPromise(obj){
var results = new obj.constructor();
var keys = Object.keys(obj);
var promises = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var promise = toPromise.call(this, obj[key]);
let results = new obj.constructor();
let keys = Object.keys(obj);
let promises = [];
for (let key of keys) {
let promise = toPromise.call(this, obj[key]);
if (promise && isPromise(promise)) defer(promise, key);
else results[key] = obj[key];
}
Expand Down Expand Up @@ -218,7 +216,7 @@ function isGenerator(obj) {
* @api private
*/
function isGeneratorFunction(obj) {
var constructor = obj.constructor;
const constructor = obj.constructor;
if (!constructor) return false;
if ('GeneratorFunction' === constructor.name || 'GeneratorFunction' === constructor.displayName) return true;
return isGenerator(constructor.prototype);
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "co",
"version": "4.6.0",
"version": "4.7.0",
"description": "generator async control flow goodness",
"keywords": [
"async",
Expand All @@ -10,17 +10,18 @@
"coroutine"
],
"devDependencies": {
"babelify": "^6.4.0",
"browserify": "^10.0.0",
"istanbul-harmony": "0",
"istanbul": "^0.4.0",
"mocha": "^2.0.0",
"mz": "^1.0.2"
"mz": "^2.1.0"
},
"scripts": {
"test": "mocha --harmony",
"test-cov": "node --harmony node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --reporter dot",
"test-travis": "node --harmony node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --reporter dot",
"test": "mocha",
"test-cov": "node node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --reporter dot",
"test-travis": "node node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --reporter dot",
"prepublish": "npm run browserify",
"browserify": "browserify index.js -o ./co-browser.js -s co"
"browserify": "browserify index.js -t babelify -o ./co-browser.js -s co"
},
"files": [
"co-browser.js",
Expand All @@ -29,7 +30,6 @@
"license": "MIT",
"repository": "tj/co",
"engines": {
"iojs": ">= 1.0.0",
"node": ">= 0.12.0"
"node": ">= 4.0"
}
}