Last active
October 26, 2017 22:14
-
-
Save LightSpeedC/a25fef9482e06c3e2dd6 to your computer and use it in GitHub Desktop.
[JavaScript] 非同期処理のコールバック地獄から抜け出す方法 ref: http://qiita.com/LightSpeedC/items/7980a6e790d6cb2d6dad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
var Channel = aa.Channel; | |
var get = require('./get'); | |
aa(function*() { | |
var ch = Channel(); | |
get('a.txt', ch); | |
var a = yield ch; | |
get('b.txt', ch); | |
var b = yield ch; | |
get('c.txt', ch); | |
var c = yield ch; | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
var Channel = aa.Channel; | |
var get = require('./get'); | |
aa(function*() { | |
var ch_a = Channel(); | |
var ch_b = Channel(); | |
var ch_c = Channel(); | |
get('a.txt', ch_a); | |
get('b.txt', ch_b); | |
get('c.txt', ch_c); | |
var a = yield ch_a; | |
var b = yield ch_b; | |
var c = yield ch_c; | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
var Channel = aa.Channel; | |
var get = require('./get'); | |
aa(function*() { | |
var ch_a = Channel(); | |
var ch_b = Channel(); | |
var ch_c = Channel(); | |
get('a.txt', ch_a); | |
get('b.txt', ch_b); | |
var a = yield ch_a; | |
var b = yield ch_b; | |
get('c.txt', ch_c); | |
var c = yield ch_c; | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
var Channel = aa.Channel; | |
var ch1 = Channel(); | |
var ch2 = Channel(); | |
// *** FIRST THREAD *** | |
aa(function*() { | |
// send | |
ch2('(a)'); | |
// receive | |
var b = yield ch1; | |
console.log(b); | |
// send | |
ch2('(c)'); | |
console.log('end of thread 1'); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); | |
// *** SECOND THREAD *** | |
aa(function*() { | |
// receive | |
var a = yield ch2; | |
// send | |
ch1('(b)'); | |
// receive | |
var c = yield ch2; | |
console.log(a + c); | |
console.log('end of thread 2'); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
var get = aa.thunkify(require('./get')); | |
aa(function*() { | |
var a = yield get('a.txt'); | |
var b = yield get('b.txt'); | |
var c = yield get('c.txt'); | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
var get = aa.thunkify(require('./get')); | |
aa(function*() { | |
var res = yield {a: get('a.txt'), | |
b: get('b.txt'), | |
c: get('c.txt')}; | |
console.log(res.a + res.b + res.c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
var get = aa.thunkify(require('./get')); | |
aa(function*() { | |
var res = yield {a: get('a.txt'), | |
b: get('b.txt')}; | |
res.c = yield get('c.txt'); | |
console.log(res.a + res.b + res.c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var get = require('./get'); | |
get('a.txt', function (err, a) { | |
get('b.txt', function (err, b) { | |
get('c.txt', function (err, c) { | |
console.log(a + b + c); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var get = require('./get'); | |
var n = 0; | |
var res = {}; | |
++n; | |
get('a.txt', function (err, a) { | |
res.a = a; | |
if (--n === 0) callback(null, res); | |
}); | |
++n; | |
get('b.txt', function (err, b) { | |
res.b = b; | |
if (--n === 0) callback(null, res); | |
}); | |
++n; | |
get('c.txt', function (err, c) { | |
res.c = c; | |
if (--n === 0) callback(null, res); | |
}); | |
function callback(err, res) { | |
console.log(res.a + res.b + res.c); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var get = require('./get'); | |
var n = 0; | |
var res = {}; | |
++n; | |
get('a.txt', function (err, a) { | |
res.a = a; | |
if (--n === 0) callback(null, res); | |
}); | |
++n; | |
get('b.txt', function (err, b) { | |
res.b = b; | |
if (--n === 0) callback(null, res); | |
}); | |
function callback(err, res) { | |
get('c.txt', function (err, c) { | |
res.c = c; | |
console.log(res.a + res.b + res.c); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
var chan = require('co-chan'); | |
var get = require('./get'); | |
co(function*() { | |
var ch = chan(); | |
get('a.txt', ch); | |
var a = yield ch; | |
get('b.txt', ch); | |
var b = yield ch; | |
get('c.txt', ch); | |
var c = yield ch; | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
var chan = require('co-chan'); | |
var get = require('./get'); | |
co(function*() { | |
var ch_a = chan(); | |
var ch_b = chan(); | |
var ch_c = chan(); | |
get('a.txt', ch_a); | |
get('b.txt', ch_b); | |
get('c.txt', ch_c); | |
var a = yield ch_a; | |
var b = yield ch_b; | |
var c = yield ch_c; | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
var chan = require('co-chan'); | |
var get = require('./get'); | |
co(function*() { | |
var ch_a = chan(); | |
var ch_b = chan(); | |
var ch_c = chan(); | |
get('a.txt', ch_a); | |
get('b.txt', ch_b); | |
var a = yield ch_a; | |
var b = yield ch_b; | |
get('c.txt', ch_c); | |
var c = yield ch_c; | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
var chan = require('co-chan'); | |
var ch = chan(); | |
// *** FIRST THREAD *** | |
co(function*() { | |
// send sync | |
yield ch('(a)'); | |
// receive | |
var b = yield ch; | |
console.log(b); | |
// send async | |
ch('(c)'); | |
console.log('end of thread 1'); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); | |
// *** SECOND THREAD *** | |
co(function*() { | |
// receive | |
var a = yield ch; | |
// send sync | |
yield ch('(b)'); | |
// receive | |
var c = yield ch; | |
console.log(a + c); | |
console.log('end of thread 2'); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var get = require('./get'); | |
function co_get(file) { | |
return function (callback) { | |
get(file, callback); | |
}; | |
} | |
// var thunkify = require('thunkify'); | |
// var co_get = thunkify(get); | |
module.exports = co_get; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var get = require('./get'); | |
function co_get(file) { | |
return new Promise(function (resolve, reject) { | |
get(file, function (err, value) { | |
if (err) reject(err); | |
else resolve(value); | |
}); | |
}); | |
} | |
module.exports = co_get; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
var get = require('./co-get'); | |
co(function*() { | |
var a = yield get('a.txt'); | |
var b = yield get('b.txt'); | |
var c = yield get('c.txt'); | |
console.log(a + b + c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
var get = require('./co-get'); | |
co(function*() { | |
var res = yield {a: get('a.txt'), | |
b: get('b.txt'), | |
c: get('c.txt')}; | |
console.log(res.a + res.b + res.c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
var get = require('./co-get'); | |
co(function*() { | |
var res = yield {a: get('a.txt'), | |
b: get('b.txt')}; | |
res.c = yield get('c.txt'); | |
console.log(res.a + res.b + res.c); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node aa-get1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node --harmony co-get1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node aa-get2.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ iojs co-get1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node aa-get3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node --harmony co-get3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node aa-chan1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node --harmony co-get2.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node aa-chan2.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ iojs co-get3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node callback-hell1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node aa-chan3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node --harmony co-chan3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node aa-chan4.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ch = Channel(); | |
ch.stream(socket); | |
// socket.on('end', ch.end); | |
// socket.on('error', ch); | |
// socket.on('readable', ch.readable); | |
var buff; | |
while (buff = yield ch) { | |
console.log(String(buff)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm i co-chan -S | |
$ node --harmony co-chan1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
aa(function*() { | |
var ary = [1, 2, 3]; | |
ary.forEach(function (val) { | |
console.log(val); | |
}); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var aa = require('aa'); | |
aa(function*() { | |
var ary = [1, 2, 3]; | |
return yield ary.map(val => function* () { | |
console.log(val); | |
yield Promise.resolve(val); | |
}); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ iojs co-chan2.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node --harmony co-chan2.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ iojs co-chan3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node --harmony co-chan3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ iojs co-chan4.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node --harmony co-chan4.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ch = chan(); | |
ch.stream(socket); | |
// socket.on('end', ch.end); | |
// socket.on('error', ch); | |
// socket.on('readable', ch.readable); | |
while (!ch.done()) { | |
var buff = yield ch; | |
if (!buff) break; | |
console.log(String(buff)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var co = require('co'); | |
co(function*() { | |
var ary = [1, 2, 3]; | |
ary.forEach(function (val) { | |
console.log(val); | |
}); | |
}).then( | |
function (val) { console.info('ok: ' + val); }, | |
function (err) { console.error('ng: ' + err); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node callback-hell2.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node callback-hell3.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function get(file, callback) { | |
console.log('file: %s...', file); | |
setTimeout(function () { | |
console.log('file: %s complete', file); | |
callback(null, '(' + file + ')'); | |
}, 200 + Math.random() * 100); | |
} | |
module.exports = get; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install aa --save |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm i aa -S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install co --save |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm i co -S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install thunkify |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment