Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

非同期javascriptの過去と未来

1,066 views

Published on

javascriptの非同期処理の過去、そして今後についてです。

Published in: Engineering
  • Be the first to comment

非同期javascriptの過去と未来

  1. 1. Past and Future of Asynchronous Javascript
  2. 2. : @brn ( ) : : Cyberagent RightSegment AI Messenger : http://abcdef.gets.b6n.ch/
  3. 3. What is Asynchronous? javascript javascript stack stack
  4. 4. What is Asynchronous? ee..gg.. sseettTTiimmeeoouutt setTimeout
  5. 5. Callback javascript someAsyncJob(function() {…});
  6. 6. jQuery.Deferred jQuery Deferred function delayHello() { var d = new $.Deferred; setTimeout(function(){ d.resolve('Hello'); }, 1000); return d.promise(); } delayHello().done(function(word) {console.log(word)})
  7. 7. Promise/A+ Nodejs Web js CommonJS Q Q.fcall(promisedStep1) .then(promisedStep2) .then(promisedStep3) .then(promisedStep4) .then(function (value4) { // Do something with value4 }) .catch(function (error) { // Handle any error from all above steps }) .done();
  8. 8. Promise Ecmascript Promise const p = new Promise((resolve, reject) => { setTimeout(() => resolve('hello')); }); p.then(word => console.log(word));
  9. 9. Problems Promise const p = new Promise(resolve => { setTimeout(() => resolve('hello')); }) p.then(word => { setTimeout(() => resolve(`${word} world`)); }) .then(word => console.log(word));
  10. 10. Generator & co Generator Ecmascript Generator co const hello = () => new Promise(resolve => { setTimeout(() => resolve("hello"), 300); }); const world = () => new Promise(resolve => { setTimeout(() => resolve("world"), 300); }); co(function* () { var hello = yield hello(); var world = yield world(); return `${hello} ${world}`; }).then(word => console.log(value));
  11. 11. async & await async await ES7 Promise async helloWorld() { const hello = await new Promise(resolve => { setTimeout(() => resolve('hello')); }); const word = await new Promise(resolve => { setTimeout(() => resolve('world')); }); console.log(`${hello} ${world}`) }
  12. 12. Asynchronous Iterators async await Asynchronous Iterators Stage3 async function* readLines(path) { let file = await fileOpen(path); try { while (!file.EOF) { yield await file.readLine(); } } finally { await file.close(); } } for await (const line of readLines(filePath)) { console.log(line); }
  13. 13. •  Babel typescript async await •  JS Bluebird •  Promise https://gist.github.com/brn/4f639a5bdb845f0aebe8e0725691a442

×