Comparing version 0.3.1 to 0.4.0
@@ -9,4 +9,2 @@ /* Copyright 2015, Wang Wenlin */ | ||
require('../').patchPromise(); | ||
// Ref: http://swannodette.github.io/2013/08/24/es6-generators-and-csp/ | ||
@@ -26,5 +24,2 @@ // | ||
db.queryPromise('SELECT 1').then(print).done(ch); | ||
var rp = yield ch; | ||
go(function* (ch2) { | ||
@@ -40,12 +35,17 @@ db.query('SELECT 1', bind(ch2, 'r3')); | ||
var rz = yield ch; | ||
console.log(rz); | ||
} catch (e) {} | ||
// Free channel | ||
var ch3 = Channel(); | ||
db.query('SELECT 1 FROM dummy', then(ch3, function (res) { ch3(null, res[0]); })); | ||
db2.query('SELECT 3', ch); | ||
var r3 = yield ch3; | ||
var r4 = yield; | ||
console.log(r3, r4); | ||
db.query('SELECT 1 FROM dummy', then(ch, function (res) { ch(null, res[0]); })); | ||
db2.query('SELECT 3', ch3); | ||
var r3 = yield; | ||
var r4 = yield ch3; | ||
// ES6 Promise | ||
var prms = db.queryPromise('SELECT 1').then(print); | ||
var rp = yield prms; | ||
var rp2 = yield prms; | ||
var rp3 = yield ES.readFilePromise('/etc/hosts'); | ||
}); |
28
index.js
@@ -8,3 +8,2 @@ /* Copyright 2015, Wang Wenlin */ | ||
exports.then = then; | ||
exports.patchPromise = patchPromise; | ||
@@ -58,2 +57,3 @@ // Ref: http://swannodette.github.io/2013/08/24/es6-generators-and-csp/ | ||
var chan = Channel([]); | ||
var promiseChan; | ||
var runq = chan; | ||
@@ -79,3 +79,3 @@ | ||
runq = iter.value || chan; | ||
if (runq.ctor_ !== Channel) runq = chan; | ||
if (runq.ctor_ !== Channel) fix(); | ||
} | ||
@@ -95,2 +95,12 @@ })(); | ||
} | ||
function fix() { | ||
if (typeof runq.then !== 'function') { | ||
runq = chan; | ||
} else { | ||
if (!promiseChan) promiseChan = Channel(); | ||
runq.then(function (val) { promiseChan(null, val); }, promiseChan); | ||
runq = promiseChan; | ||
} | ||
} | ||
} | ||
@@ -148,15 +158,1 @@ | ||
} | ||
/** | ||
* Patch ES6 Promise to add non-standard Promise#done() support | ||
* @param {Function(e, v)} cb - node.js style callback | ||
* @returns {Promise} - chaining promise | ||
*/ | ||
function patchPromise() { | ||
Promise.prototype.done = function (cb) { | ||
return this.then( | ||
function (val) { cb(null, val); }, | ||
function (err) { cb(err); } | ||
); | ||
}; | ||
} |
{ | ||
"name": "gojs", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "go.js, Golang like channels, goroutine and go.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,4 +10,2 @@ go.js, Golang like channels, goroutine and go. | ||
require('gojs').patchPromise(); | ||
// Ref: http://swannodette.github.io/2013/08/24/es6-generators-and-csp/ | ||
@@ -27,5 +25,2 @@ // | ||
db.queryPromise('SELECT 1').then(print).done(ch); | ||
var rp = yield ch; | ||
go(function* (ch2) { | ||
@@ -41,13 +36,18 @@ db.query('SELECT 1', bind(ch2, 'r3')); | ||
var rz = yield ch; | ||
console.log(rz); | ||
} catch (e) {} | ||
// Free channel | ||
var ch3 = Channel(); | ||
db.query('SELECT 1 FROM dummy', then(ch3, function (res) { ch3(null, res[0]); })); | ||
db2.query('SELECT 3', ch); | ||
var r3 = yield ch3; | ||
var r4 = yield; | ||
console.log(r3, r4); | ||
db.query('SELECT 1 FROM dummy', then(ch, function (res) { ch(null, res[0]); })); | ||
db2.query('SELECT 3', ch3); | ||
var r3 = yield; | ||
var r4 = yield ch3; | ||
// ES6 Promise | ||
var prms = db.queryPromise('SELECT 1').then(print); | ||
var rp = yield prms; | ||
var rp2 = yield prms; | ||
var rp3 = yield ES.readFilePromise('/etc/hosts'); | ||
}); | ||
``` |
@@ -12,4 +12,2 @@ /* Copyright 2015, Wang Wenlin */ | ||
require('../').patchPromise(); | ||
describe('go.js', function () { | ||
@@ -100,3 +98,3 @@ describe('Channel', function () { | ||
it('throw in goroutine would crash if without protects', function () { | ||
it('throw in goroutine would crash if without any protect', function () { | ||
assert.throws(function () { | ||
@@ -144,3 +142,3 @@ go(function* () { throw Error('err'); }); | ||
it('two goroutine communicate thru channel', function (done) { | ||
it('two goroutine can communicate thru channel', function (done) { | ||
var ch1 = go(function* (ch1) { | ||
@@ -156,2 +154,27 @@ var c = yield; | ||
}); | ||
it('support yield ES6 promise to read from it', function (done) { | ||
go(function* (chan) { | ||
var prms = new Promise(function (resolve, reject) { | ||
process.nextTick(function () { | ||
resolve('resolved'); | ||
}); | ||
}); | ||
assert.equal((yield prms), 'resolved'); | ||
assert.equal((yield prms), 'resolved'); | ||
assert.equal((yield prms), 'resolved'); | ||
var prms2 = new Promise(function (resolve, reject) { | ||
process.nextTick(function () { | ||
reject(Error('rejected')); | ||
}); | ||
}); | ||
try { | ||
yield prms2; | ||
done(Error('prms2 should throw')); | ||
} catch (e) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
@@ -212,26 +235,2 @@ | ||
}); | ||
describe('patchPromise()', function () { | ||
it('merge resolve and reject callbacks into one', function (done) { | ||
var prms = new Promise(function (resolve, reject) { resolve(1); }); | ||
var prms2 = new Promise(function (resolve, reject) { reject(Error('err')); }); | ||
prms.done(function (e, val) { | ||
try { | ||
assert.equal(e, null); | ||
assert.equal(val, 1); | ||
} catch (e) { | ||
return done(e); | ||
} | ||
prms2.done(function (e, val) { | ||
try { | ||
assert.equal(e.message, 'err'); | ||
} catch (e) { | ||
return done(e); | ||
} | ||
}).then(done); | ||
}); | ||
}); | ||
}); | ||
}); |
13177
381