From ce22f12f5874d7f3e52a210d5391c6bec241015b Mon Sep 17 00:00:00 2001 From: pana Date: Sat, 24 Oct 2015 16:44:35 +0800 Subject: [PATCH 1/3] rewrite with es2016 --- History.md | 6 ++++++ Readme.md | 12 +----------- component.json | 2 +- index.js | 32 +++++++++++++++----------------- package.json | 15 +++++++-------- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/History.md b/History.md index 68fbb15..ecbf6a6 100644 --- a/History.md +++ b/History.md @@ -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 ================== diff --git a/Readme.md b/Readme.md index c1d4882..49acc28 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/component.json b/component.json index d53deab..86b665d 100644 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { "name": "co", - "version": "4.4.0", + "version": "4.7.0", "description": "generator async control flow goodness", "keywords": [ "async", diff --git a/index.js b/index.js index 87ba8ba..faf8259 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ - +'use strict'; /** * slice() reference. */ -var slice = Array.prototype.slice; +const slice = Array.prototype.slice; /** * Expose `co`. @@ -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. @@ -60,7 +60,7 @@ function co(gen) { */ function onFulfilled(res) { - var ret; + let ret; try { ret = gen.next(res); } catch (e) { @@ -76,7 +76,7 @@ function co(gen) { */ function onRejected(err) { - var ret; + let ret; try { ret = gen.throw(err); } catch (e) { @@ -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)}"`)); } }); } @@ -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); @@ -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]; } @@ -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); diff --git a/package.json b/package.json index 28244ea..b054430 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "co", - "version": "4.6.0", + "version": "4.7.0", "description": "generator async control flow goodness", "keywords": [ "async", @@ -11,14 +11,14 @@ ], "devDependencies": { "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" }, @@ -29,7 +29,6 @@ "license": "MIT", "repository": "tj/co", "engines": { - "iojs": ">= 1.0.0", - "node": ">= 0.12.0" + "node": ">= 4.0" } } From 5b966a590a20de741d0b35050ed3844309525f1a Mon Sep 17 00:00:00 2001 From: pana Date: Sat, 24 Oct 2015 16:57:13 +0800 Subject: [PATCH 2/3] add babelfiy --- .travis.yml | 3 +-- package.json | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 43b66de..850257e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/package.json b/package.json index b054430..292caae 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "coroutine" ], "devDependencies": { + "babelify": "^6.4.0", "browserify": "^10.0.0", "istanbul": "^0.4.0", "mocha": "^2.0.0", @@ -20,7 +21,7 @@ "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", From 5d2b77a17d10566bcd1155f4e304a3b9351c88fb Mon Sep 17 00:00:00 2001 From: pana Date: Sat, 24 Oct 2015 17:02:45 +0800 Subject: [PATCH 3/3] update readme --- Readme.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Readme.md b/Readme.md index 49acc28..be4dbbc 100644 --- a/Readme.md +++ b/Readme.md @@ -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); @@ -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); }); @@ -62,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); @@ -126,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), @@ -141,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), }; @@ -172,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); });