Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript Assíncrono

JavaScript Assíncrono

Palestra sobre javascript usando promises.

Avatar for Vinicius Baggio Fuentes

Vinicius Baggio Fuentes

September 14, 2013
Tweet

More Decks by Vinicius Baggio Fuentes

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. Grandes sistemas que usam programação funcional tendem a parecer como

    uma montadora de automóveis Wednesday, September 18, 13
  14. 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
  15. 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
  16. 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
  17. Q • Promises/A+ • converter node.js-style callbacks p/ promises •

    all/spread, progress noti cation Wednesday, September 18, 13