Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
JavaScript Assíncrono
Search
Vinicius Baggio Fuentes
September 14, 2013
Programming
2
390
JavaScript Assíncrono
Palestra sobre javascript usando promises.
Vinicius Baggio Fuentes
September 14, 2013
Tweet
Share
More Decks by Vinicius Baggio Fuentes
See All by Vinicius Baggio Fuentes
Programação funcional para fazer Ruby melhor
vinibaggio
1
720
Other Decks in Programming
See All in Programming
はじめてのMaterial3 Expressive
ym223
2
1.1k
Local Peer-to-Peer APIはどのように使われていくのか?
hal_spidernight
2
330
ててべんす独演会〜Flowの全てを語ります〜
tbsten
1
200
TokyoR#119 bignners session2 Visualization
kotatyamtema
0
110
「社内LT会」を1年続けてみた! / Our Year-Long Journey of Internal Lightning Talks
mackey0225
1
110
ABEMAモバイルアプリが Kotlin Multiplatformと歩んだ5年 ─ 導入と運用、成功と課題 / iOSDC 2025
akkyie
0
210
Playwrightはどのようにクロスブラウザをサポートしているのか
yotahada3
7
2.1k
Deep Dive into Kotlin Flow
jmatsu
1
430
Model Pollution
hschwentner
1
170
Let's Write a Train Tracking Algorithm
twocentstudios
0
200
iOSDC.pdf
chronos2500
2
570
AccessorySetupKitで実現するシームレスなペアリング体験 / Seamless pairing with AccessorySetupKit
nekowen
0
180
Featured
See All Featured
For a Future-Friendly Web
brad_frost
180
9.9k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Why Our Code Smells
bkeepers
PRO
339
57k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Site-Speed That Sticks
csswizardry
10
850
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
The Power of CSS Pseudo Elements
geoffreycrofte
78
6k
Agile that works and the tools we love
rasmusluckow
330
21k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
188
55k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
61k
Transcript
JavaScript assíncrono then Vinícius Baggio Fuentes Wednesday, September 18, 13
Words Matter Wednesday, September 18, 13
Wednesday, September 18, 13
User profile Wednesday, September 18, 13
User profile Post data Wednesday, September 18, 13
User profile Post data Notes Wednesday, September 18, 13
User profile Post data Notes read later collection collab Wednesday,
September 18, 13
read next recommended rec. stats further reading Wednesday, September 18,
13
várias formas de resolver • SQL Joins absurdos • Denormalização*
dos dados • Materialized Views (???) • Buscar em etapa (pipeline) * Tradução tosca!! Wednesday, September 18, 13
Denormalização + Pipeline* * Esta é apenas uma simulação inventada
e qualquer semelhança com a realidade é um mero acidente. Post + Anotações + Further reading Perfil, foto etc Recomendações + stats Read next Compõe e serve Wednesday, September 18, 13
fetchPostData(id, function (err, post) { fetchProfileData(post.profileId, function (err, profile) {
fetchRecommendation(id, function (err, recStats) { fetchReadNext(id, function (err, readNext) { render({ post: renderPartial(post), profile: renderPartial(profile), recStats: renderPartial(recStats), readNext: renderPartial(readNext), }) }) }) }) }) Wednesday, September 18, 13
fetchPostData(id, function (err, post) { fetchProfileData(post.profileId, function (err, profile) {
fetchRecommendation(id, function (err, recStats) { fetchReadNext(id, function (err, readNext) { render({ post: renderPartial(post), profile: renderPartial(profile), recStats: renderPartial(recStats), readNext: renderPartial(readNext), }) }) }) }) }) LOL NODE Wednesday, September 18, 13
fetchPostData(id, function (err, post) { fetchProfileData(post.profileId, function (err, profile) {
fetchRecommendation(id, function (err, recStats) { fetchReadNext(id, function (err, readNext) { render({ post: renderPartial(post), profile: renderPartial(profile), recStats: renderPartial(recStats), readNext: renderPartial(readNext), }) }) }) }) }) Wednesday, September 18, 13
Post + Anotações + Further reading Perfil, foto etc Recomendações
+ stats Read next Renderiza e serve Denormalização + Pipeline + concorrência Wednesday, September 18, 13
var data = {} fetchPostData(id, function (err, post) { data.post
= post fetchProfileData(post.profileId, function (err, profile) { data.profile = renderPartial(profile) }) fetchRecommendation(id, function (err, recStats) { data.recStats = renderPartial(recStats) }) fetchReadNext(id, function (err, readNext) { data.readNext = renderPartial(readNext) }) }) process.nextTick(function tryRenderData () { if (data.post && data.profile && data.recStats && data.readNext) { render(data) } else { process.nextTick(tryRenderData) } }) Wednesday, September 18, 13
Futures Wednesday, September 18, 13
var id = '7c95f3526713' var post = future(fetchPostData, id) var
recStats = future(fetchRecommendation, id) var readNext = future(fetchReadNext, id) var profile = future(fetchProfileData, post.realize().profileId) var postPartial = future(renderPartial, post) var recStatsPartial = future(renderPartial, recStats) var readNextPartial = future(renderPartial, readNext) var profilePartial = future(renderPartial, profile) render({ post: postPartial.realize(), recStats: recStatsPartial.realize(), profile: profilePartial.realize(), readNext: profilePartial.realize(), }) Wednesday, September 18, 13
future() realize() Wednesday, September 18, 13
var id = '7c95f3526713' var post = future(fetchPostData, id) var
recStats = future(fetchRecommendation, id) var readNext = future(fetchReadNext, id) var profile = future(fetchProfileData, post.realize().profileId) var postPartial = future(renderPartial, post) var recStatsPartial = future(renderPartial, recStats) var readNextPartial = future(renderPartial, readNext) var profilePartial = future(renderPartial, profile) render({ post: postPartial.realize(), recStats: recStatsPartial.realize(), profile: profilePartial.realize(), readNext: profilePartial.realize(), }) Cenário A: função demora a retornar retorna rápido { t realize() bloqueia retorna rápido { realize() bloqueia Wednesday, September 18, 13
var id = '7c95f3526713' var post = future(fetchPostData, id) var
recStats = future(fetchRecommendation, id) var readNext = future(fetchReadNext, id) var profile = future(fetchProfileData, post.realize().profileId) var postPartial = future(renderPartial, post) var recStatsPartial = future(renderPartial, recStats) var readNextPartial = future(renderPartial, readNext) var profilePartial = future(renderPartial, profile) render({ post: postPartial.realize(), recStats: recStatsPartial.realize(), profile: profilePartial.realize(), readNext: profilePartial.realize(), }) Cenário B: função executa rápido retorna rápido { t realize() retorna rápido retorna rápido { realize() retorna rápido Wednesday, September 18, 13
Promises Wednesday, September 18, 13
var id = '7c95f3526713' var postPromise = fcall(fetchPostData, id) var
recStatsPromise = fcall(fetchRecommendation, id).then(renderPartial) var readNextPromise = fcall(fetchReadNext, id).then(renderPartial) var postPartialPromise = postPromise.then(renderPartial) var profilePromise = postPromise.then(function (post) { return fcall(fetchProfileData, post.profileId) }) all(postPartialPromise, recStatsPromise, readNextPromise, profilePromise) .then(function (postPartial, recStatsPartial, readNextPartial, profilePartial) { render({ post: postPartial, recStats: recStatsPartial, profile: profilePartial, readNext: profilePartial, }) }) Wednesday, September 18, 13
var id = '7c95f3526713' var postPromise = fetchPostData(id) var recStatsPromise
= fetchRecommendation(id).then(renderPartial) var readNextPromise = fetchReadNext(id).then(renderPartial) var postPartialPromise = postPromise.then(renderPartial) var profilePromise = postPromise.then(function (post) { return fetchProfileData(post.profileId) }) all(postPartialPromise, recStatsPromise, readNextPromise, profilePromise) .then(function (postPartial, recStatsPartial, readNextPartial, profilePartial) { render({ post: postPartial, recStats: recStatsPartial, profile: profilePartial, readNext: profilePartial, }) }) Wednesday, September 18, 13
Grandes sistemas que usam programação funcional tendem a parecer como
uma montadora de automóveis Wednesday, September 18, 13
function createPost(req, res) { reqFormData(req, 'post') .then(validateNewPost) .then(createPost) .then(function (post)
{ res.render('posts.show', {post: post}) }) .fail(function (error) { res.render422('posts.new', error) }) } Wednesday, September 18, 13
function createPost(req, res) { reqFormData(req, 'post') .then(validateNewPost) .then(createPost) .then(function (post)
{ res.render('posts.show', {post: post}) }) .fail(function (error) { res.render422('posts.new', error) }) } function validatePost(formData) { return Q.all( _validateTitle(formData), _validateBody(formData) ).then(function () { return formData }) } Wednesday, September 18, 13
function createPost(req, res) { reqFormData(req, 'post') .then(validateNewPost) .then(createPost) .then(function (post)
{ res.render('posts.show', {post: post}) }) .fail(function (error) { res.render422('posts.new', error) }) } MONADS! function validatePost(formData) { return Q.all( _validateTitle(formData), _validateBody(formData) ).then(function () { return formData }) } Wednesday, September 18, 13
ƒ wrap unwrap ƒ ƒ Wednesday, September 18, 13
Q • Promises/A+ • converter node.js-style callbacks p/ promises •
all/spread, progress noti cation Wednesday, September 18, 13
Q + _ + js = functional programming pro-style Wednesday,
September 18, 13
Functional JavaScript Wednesday, September 18, 13
Perguntas? Wednesday, September 18, 13
Créditos de imagens: http://www.flickr.com/photos/thomashawk/292253202 Wednesday, September 18, 13