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
400
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
Cap'n Webについて
yusukebe
0
160
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
160
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
10
1.6k
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
540
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
110
Claude Codeの「Compacting Conversation」を体感50%減! CLAUDE.md + 8 Skills で挑むコンテキスト管理術
kmurahama
1
730
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
480
Go コードベースの構成と AI コンテキスト定義
andpad
0
160
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
5.2k
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
640
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
4
730
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
250
Featured
See All Featured
Optimizing for Happiness
mojombo
379
70k
Embracing the Ebb and Flow
colly
88
4.9k
Rails Girls Zürich Keynote
gr2m
95
14k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
61
48k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
46
Paper Plane
katiecoart
PRO
0
45k
The Curious Case for Waylosing
cassininazir
0
200
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
81
Crafting Experiences
bethany
0
29
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Optimising Largest Contentful Paint
csswizardry
37
3.6k
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