Last active
October 25, 2017 12:46
ES2017 async/await + Promise で解決できる事、とES2015(ES6) generators (yield) + Promise + npm aa (async-await) で解決できる事 ref: http://qiita.com/LightSpeedC/items/95e3db59276e5d1d1a0d
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'; | |
require('babel-polyfill'); // おまじない | |
//require('regenerator').runtime(); | |
console.log('main: start'); | |
main().then(function (val) { | |
console.log('main: finish:', val); | |
}); | |
async function main() { | |
var result; | |
console.log('main: started'); | |
// シーケンシャル処理(逐次処理) | |
result = await sleep(200, 'a1'); | |
console.log('main-a1: sleep: a1 =', result); | |
result = await sleep(100, 'a2'); | |
console.log('main-a2: sleep: a2 =', result); | |
result = await sleep(300, 'a3'); | |
console.log('main-a3: sleep: a3 =', result); | |
// パラレル処理(並行処理) | |
result = await Promise.all([sleep(200, 'b1'), sleep(100, 'b2'), sleep(300, 'b3')]); | |
console.log('main-b : Promise.all([promises]): [b1, b2, b3] =', stringify(result)); | |
// asyncなsub()をawaitで呼ぶ | |
result = await sub('c'); | |
console.log('main-c : sub(c) =', result); | |
// asyncなsub()を並行処理で呼ぶ | |
result = await Promise.all([sub('d1'), sub('d2')]); | |
console.log('main-d : Promise.all([promises]): [d1, d2] =', stringify(result)); | |
return 'return value!'; | |
} | |
async function sub(val) { | |
await sleep(100, val + '-x1'); | |
console.log('sub-' + val + '-x1: sleep: ' + val + '-x1'); | |
await sleep(100, val + '-x2'); | |
console.log('sub-' + val + '-x2: sleep: ' + val + '-x2'); | |
return val; | |
} | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} | |
function stringify(object) { | |
var str = ''; | |
if (object instanceof Array) { | |
for (var i = 0, n = object.length; i < n; ++i) | |
str += (str ? ', ' : '') + object[i] | |
return '[' + str + ']'; | |
} | |
else { | |
for (var key in object) | |
str += (str ? ', ' : '') + key + ':' + object[key] | |
return '{' + str + '}'; | |
} | |
} |
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
<!doctype html> | |
<meta charset="UTF-8"> | |
<script src="https://lightspeedworks.github.io/console-to-window/console-to-window.js"></script> | |
<script src="https://lightspeedworks.github.io/promise-thunk/promise-thunk.js"></script> | |
<script src="https://lightspeedworks.github.io/aa/aa.js"></script> | |
<script src="es2015-async-await-ex.js"></script> |
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
// require('regenerator').runtime() // babel & browserifyするなら入れておく | |
// var aa = require('aa'); | |
var aa = (this && this.aa) || require('aa'); // 普通は var aa = require('aa'); で良い | |
var Promise = aa.Promise; // native Promiseがあれば不要 | |
console.log('main: start'); | |
aa(main()).then(function (val) { | |
console.log('main: finish:', val); | |
}); | |
function *main() { | |
var result; | |
console.log('main: started'); | |
// シーケンシャル処理(逐次処理) | |
result = yield sleep(200, 'a1'); | |
console.log('main-a1: sleep: a1 =', result); | |
result = yield sleep(100, 'a2'); | |
console.log('main-a2: sleep: a2 =', result); | |
result = yield sleep(300, 'a3'); | |
console.log('main-a3: sleep: a3 =', result); | |
// パラレル処理(並行処理) ... 配列やオブジェクトで結果を取得 | |
result = yield [sleep(200, 'b1'), sleep(100, 'b2'), sleep(300, 'b3')]; | |
console.log('main-b : parallel Array :', stringify(result)); | |
result = yield {x:sleep(200, 'c1'), y:sleep(100, 'c2'), z:sleep(300, 'c3')}; | |
console.log('main-c : parallel Object:', stringify(result)); | |
result = yield Promise.all([sleep(200, 'd1'), sleep(100, 'd2'), sleep(300, 'd3')]); | |
console.log('main-d : Promise.all([promises,...]): [d1, d2, d3] =', stringify(result)); | |
// generatorのsub()をyieldで呼ぶ | |
result = yield sub('e'); | |
console.log('main-e : sub(e) =', result); | |
// パラレル処理(並行処理) ... 配列やオブジェクトで結果を取得 | |
// generatorのsub()を並行処理で呼ぶ ... 配列 | |
result = yield [sub('f1'), sub('f2')]; | |
console.log('main-f : [generator,...]: [f1, f2] =', stringify(result)); | |
// generatorのsub()を並行処理で呼ぶ ... オブジェクト | |
result = yield {x:sub('g1'), y:sub('g2')}; | |
console.log('main-g : {x:generator, y:...}: {x:g1, y:g2} =', stringify(result)); | |
// 必要ないけど無理やりgeneratorのsub()をpromiseにしてみた | |
result = yield Promise.all([aa(sub('h1')), aa(sub('h2'))]); | |
console.log('main-h : Promise.all([aa(generator),...]): [h1, h2] =', stringify(result)); | |
return 'return value!'; | |
} | |
function *sub(val) { | |
yield sleep(100, val + '-x1'); | |
console.log('sub-' + val + '-x1: sleep: ' + val + '-x1'); | |
yield sleep(100, val + '-x2'); | |
console.log('sub-' + val + '-x2: sleep: ' + val + '-x2'); | |
return val; | |
} | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} | |
function stringify(object) { | |
var str = ''; | |
if (object instanceof Array) { | |
for (var i = 0, n = object.length; i < n; ++i) | |
str += (str ? ', ' : '') + object[i] | |
return '[' + str + ']'; | |
} | |
else { | |
for (var key in object) | |
str += (str ? ', ' : '') + key + ':' + object[key] | |
return '{' + str + '}'; | |
} | |
} |
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 aa = require('aa'), Channel = aa.Channel; | |
var Executors = require('executors'); | |
aa(function *main() { | |
var parallel9 = []; | |
console.time('parallel9'); | |
for (var i = 0; i < 9; ++i) | |
parallel9.push(sleep(100, i)); | |
yield parallel9; | |
console.timeEnd('parallel9'); | |
var executors3 = Executors(3); | |
var parallel3 = []; | |
console.time('parallel3'); | |
for (var i = 0; i < 9; ++i) | |
parallel3.push(executors3(sleep, 100, i)); | |
yield parallel3; | |
console.timeEnd('parallel3'); | |
}); | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} |
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 aa = require('aa'); | |
var Channel = aa.Channel; | |
console.log('main: start'); | |
aa(main()).then(function (val) { | |
console.log('main: finish:', val); | |
}); | |
function *main() { | |
var result; | |
console.log('main: started'); | |
var chan = Channel(); | |
yield [ | |
function *() { | |
console.log('main:', yield sleep(100, 'a1')); | |
console.log('main:', yield sleep(200, 'a2')); | |
chan('a1-a2'); //----------------------------------+ | |
console.log('main:', yield sleep(300, 'a3')); // | | |
}, // | | |
function *() { // | | |
console.log('main:', yield sleep(200, 'b1')); // | | |
console.log('main:', yield chan, '-> b'); // <------+ | |
console.log('main:', yield sleep(200, 'b2')); | |
} | |
]; | |
return 'return value!'; | |
} | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} |
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 aa = require('aa'), Channel = aa.Channel; | |
aa(function *main() { | |
var chan = Channel(); | |
var countDown = 10; | |
var interval = setInterval(function () { chan(countDown--); }, 100); | |
var val; | |
while (val = yield chan) | |
console.log('count:', val); | |
clearInterval(interval); | |
}); |
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 fs = require('fs'); | |
var aa = require('aa'), Channel = aa.Channel; | |
aa(function *main() { | |
var chan = Channel().stream(fs.createReadStream('es2015-stream-ex.js', {encoding: 'utf8'})); | |
var writer = fs.createWriteStream('es2015-stream-ex.log', {encoding: 'utf8'}) | |
var buff; | |
while (buff = yield chan) | |
writer.write(buff); | |
writer.end(); | |
}); |
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'; | |
//require('babel-polyfill'); // おまじない | |
//require('regenerator').runtime(); | |
console.log('main: start'); | |
main().then(function (val) { | |
console.log('main: finish:', val); | |
}); | |
async function main() { | |
var result; | |
console.log('main: started'); | |
// シーケンシャル処理(逐次処理) | |
result = await sleep(200, 'a1'); | |
console.log('main-a1: sleep: a1 =', result); | |
result = await sleep(100, 'a2'); | |
console.log('main-a2: sleep: a2 =', result); | |
result = await sleep(300, 'a3'); | |
console.log('main-a3: sleep: a3 =', result); | |
// パラレル処理(並行処理) | |
result = await Promise.all([sleep(200, 'b1'), sleep(100, 'b2'), sleep(300, 'b3')]); | |
console.log('main-b : Promise.all([promises]): [b1, b2, b3] =', stringify(result)); | |
// asyncなsub()をawaitで呼ぶ | |
result = await sub('c'); | |
console.log('main-c : sub(c) =', result); | |
// asyncなsub()を並行処理で呼ぶ | |
result = await Promise.all([sub('d1'), sub('d2')]); | |
console.log('main-d : Promise.all([promises]): [d1, d2] =', stringify(result)); | |
return 'return value!'; | |
} | |
async function sub(val) { | |
await sleep(100, val + '-x1'); | |
console.log('sub-' + val + '-x1: sleep: ' + val + '-x1'); | |
await sleep(100, val + '-x2'); | |
console.log('sub-' + val + '-x2: sleep: ' + val + '-x2'); | |
return val; | |
} | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} | |
function stringify(object) { | |
var str = ''; | |
if (object instanceof Array) { | |
for (var i = 0, n = object.length; i < n; ++i) | |
str += (str ? ', ' : '') + object[i] | |
return '[' + str + ']'; | |
} | |
else { | |
for (var key in object) | |
str += (str ? ', ' : '') + key + ':' + object[key] | |
return '{' + str + '}'; | |
} | |
} |
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
<!doctype html> | |
<meta charset="UTF-8"> | |
<script src="https://lightspeedworks.github.io/console-to-window/console-to-window.js"></script> | |
<script src="https://lightspeedworks.github.io/promise-thunk/promise-thunk.js"></script> | |
<script src="https://lightspeedworks.github.io/aa/aa.js"></script> | |
<script src="es6-async-await-ex.js"></script> |
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
// require('regenerator').runtime() // babel & browserifyするなら入れておく | |
// var aa = require('aa'); | |
var aa = (this && this.aa) || require('aa'); // 普通は var aa = require('aa'); で良い | |
var Promise = aa.Promise; // native Promiseがあれば不要 | |
console.log('main: start'); | |
aa(main()).then(function (val) { | |
console.log('main: finish:', val); | |
}); | |
function *main() { | |
var result; | |
console.log('main: started'); | |
// シーケンシャル処理(逐次処理) | |
result = yield sleep(200, 'a1'); | |
console.log('main-a1: sleep: a1 =', result); | |
result = yield sleep(100, 'a2'); | |
console.log('main-a2: sleep: a2 =', result); | |
result = yield sleep(300, 'a3'); | |
console.log('main-a3: sleep: a3 =', result); | |
// パラレル処理(並行処理) ... 配列やオブジェクトで結果を取得 | |
result = yield [sleep(200, 'b1'), sleep(100, 'b2'), sleep(300, 'b3')]; | |
console.log('main-b : parallel Array :', stringify(result)); | |
result = yield {x:sleep(200, 'c1'), y:sleep(100, 'c2'), z:sleep(300, 'c3')}; | |
console.log('main-c : parallel Object:', stringify(result)); | |
result = yield Promise.all([sleep(200, 'd1'), sleep(100, 'd2'), sleep(300, 'd3')]); | |
console.log('main-d : Promise.all([promises,...]): [d1, d2, d3] =', stringify(result)); | |
// generatorのsub()をyieldで呼ぶ | |
result = yield sub('e'); | |
console.log('main-e : sub(e) =', result); | |
// パラレル処理(並行処理) ... 配列やオブジェクトで結果を取得 | |
// generatorのsub()を並行処理で呼ぶ ... 配列 | |
result = yield [sub('f1'), sub('f2')]; | |
console.log('main-f : [generator,...]: [f1, f2] =', stringify(result)); | |
// generatorのsub()を並行処理で呼ぶ ... オブジェクト | |
result = yield {x:sub('g1'), y:sub('g2')}; | |
console.log('main-g : {x:generator, y:...}: {x:g1, y:g2} =', stringify(result)); | |
// 必要ないけど無理やりgeneratorのsub()をpromiseにしてみた | |
result = yield Promise.all([aa(sub('h1')), aa(sub('h2'))]); | |
console.log('main-h : Promise.all([aa(generator),...]): [h1, h2] =', stringify(result)); | |
return 'return value!'; | |
} | |
function *sub(val) { | |
yield sleep(100, val + '-x1'); | |
console.log('sub-' + val + '-x1: sleep: ' + val + '-x1'); | |
yield sleep(100, val + '-x2'); | |
console.log('sub-' + val + '-x2: sleep: ' + val + '-x2'); | |
return val; | |
} | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} | |
function stringify(object) { | |
var str = ''; | |
if (object instanceof Array) { | |
for (var i = 0, n = object.length; i < n; ++i) | |
str += (str ? ', ' : '') + object[i] | |
return '[' + str + ']'; | |
} | |
else { | |
for (var key in object) | |
str += (str ? ', ' : '') + key + ':' + object[key] | |
return '{' + str + '}'; | |
} | |
} |
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 aa = require('aa'), Channel = aa.Channel; | |
var Executors = require('executors'); | |
aa(function *main() { | |
var parallel9 = []; | |
console.time('parallel9'); | |
for (var i = 0; i < 9; ++i) | |
parallel9.push(sleep(100, i)); | |
yield parallel9; | |
console.timeEnd('parallel9'); | |
var executors3 = Executors(3); | |
var parallel3 = []; | |
console.time('parallel3'); | |
for (var i = 0; i < 9; ++i) | |
parallel3.push(executors3(sleep, 100, i)); | |
yield parallel3; | |
console.timeEnd('parallel3'); | |
}); | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} |
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 aa = require('aa'); | |
var Channel = aa.Channel; | |
console.log('main: start'); | |
aa(main()).then(function (val) { | |
console.log('main: finish:', val); | |
}); | |
function *main() { | |
var result; | |
console.log('main: started'); | |
var chan = Channel(); | |
yield [ | |
function *() { | |
console.log('main:', yield sleep(100, 'a1')); | |
console.log('main:', yield sleep(200, 'a2')); | |
chan('a1-a2'); //----------------------------------+ | |
console.log('main:', yield sleep(300, 'a3')); // | | |
}, // | | |
function *() { // | | |
console.log('main:', yield sleep(200, 'b1')); // | | |
console.log('main:', yield chan, '-> b'); // <------+ | |
console.log('main:', yield sleep(200, 'b2')); | |
} | |
]; | |
return 'return value!'; | |
} | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} |
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 aa = require('aa'), Channel = aa.Channel; | |
aa(function *main() { | |
var chan = Channel(); | |
var countDown = 10; | |
var interval = setInterval(function () { chan(countDown--); }, 100); | |
var val; | |
while (val = yield chan) | |
console.log('count:', val); | |
clearInterval(interval); | |
}); |
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 fs = require('fs'); | |
var aa = require('aa'), Channel = aa.Channel; | |
aa(function *main() { | |
var chan = Channel().stream(fs.createReadStream('es6-stream-ex.js', {encoding: 'utf8'})); | |
var writer = fs.createWriteStream('es6-stream-ex.log', {encoding: 'utf8'}) | |
var buff; | |
while (buff = yield chan) | |
writer.write(buff); | |
writer.end(); | |
}); |
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'; | |
require('babel-polyfill'); // おまじない | |
//require('regenerator').runtime(); | |
console.log('main: start'); | |
main().then(function (val) { | |
console.log('main: finish:', val); | |
}); | |
async function main() { | |
var result; | |
console.log('main: started'); | |
// シーケンシャル処理(逐次処理) | |
result = await sleep(200, 'a1'); | |
console.log('main-a1: sleep: a1 =', result); | |
result = await sleep(100, 'a2'); | |
console.log('main-a2: sleep: a2 =', result); | |
result = await sleep(300, 'a3'); | |
console.log('main-a3: sleep: a3 =', result); | |
// パラレル処理(並行処理) | |
result = await Promise.all([sleep(200, 'b1'), sleep(100, 'b2'), sleep(300, 'b3')]); | |
console.log('main-b : Promise.all([promises]): [b1, b2, b3] =', stringify(result)); | |
// asyncなsub()をawaitで呼ぶ | |
result = await sub('c'); | |
console.log('main-c : sub(c) =', result); | |
// asyncなsub()を並行処理で呼ぶ | |
result = await Promise.all([sub('d1'), sub('d2')]); | |
console.log('main-d : Promise.all([promises]): [d1, d2] =', stringify(result)); | |
return 'return value!'; | |
} | |
async function sub(val) { | |
await sleep(100, val + '-x1'); | |
console.log('sub-' + val + '-x1: sleep: ' + val + '-x1'); | |
await sleep(100, val + '-x2'); | |
console.log('sub-' + val + '-x2: sleep: ' + val + '-x2'); | |
return val; | |
} | |
function sleep(msec, val) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, msec, val); | |
}); | |
} | |
function stringify(object) { | |
var str = ''; | |
if (object instanceof Array) { | |
for (var i = 0, n = object.length; i < n; ++i) | |
str += (str ? ', ' : '') + object[i] | |
return '[' + str + ']'; | |
} | |
else { | |
for (var key in object) | |
str += (str ? ', ' : '') + key + ':' + object[key] | |
return '{' + str + '}'; | |
} | |
} |
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 --save aa | |
$ node es2015-async-await-ex |
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 fs = require('fs'); | |
// 単独のメソッドを aa で yield できる様にする。 | |
aa.promisify(fs, 'exists', {suffix: 'A'}); | |
// または | |
aa.thunkify(fs, 'exists', {suffix: 'A'}); | |
// オブジェクトのメソッド群を aa で yield できる様にする。 | |
aa.promisifyAll(fs, {suffix: 'A'}); | |
// または | |
aa.thunkifyAll(fs, {suffix: 'A'}); | |
if (yield fs.existsA('file.txt')) { | |
var buff = yield fs.readFileA('file.txt', {encoding: 'utf8'}); | |
} | |
// 例えば postgres の場合は、以下の様な感じだ。 | |
var pg = require('pg'); | |
aa.promisifyAll(pg.constructor.prototype); | |
aa.promisifyAll(pg.Client.prototype); | |
// etc... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment