Slide 1

Slide 1 text

promises && js2015 felipe bernardes github.com/felipebernardes

Slide 2

Slide 2 text

http://felipebernardes.github.io #votorantim http://tegra.me 2

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

execution stack && contexts

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

console.log(‘1’); setTimeout(() => console.log(‘2’), 0); console.log(‘3’); setTimeout(() => console.log(‘4’), 1000); //1324

Slide 7

Slide 7 text

why promise?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

● no more callback hell || pyramid of d00m ○ better performance ● cleaner && less code ○ better to undestand ○ easier to maintain ○ responsabilities && modularization ● better error handling

Slide 10

Slide 10 text

why not promise?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

how to promise

Slide 13

Slide 13 text

doThis(param) .then( response => this.someAttr = param);

Slide 14

Slide 14 text

( ... ) //syncronous code doThis(param) .then( response => this.someAttr = param); ( ... ) //syncronous code

Slide 15

Slide 15 text

doThis(param) .then( response => this.someAttr = param) .catch ( errorResponse => console.log(errorResponse) );

Slide 16

Slide 16 text

all && race

Slide 17

Slide 17 text

all http://jsfiddle.net/jsengel/mc3p01nb/ then after all promises returned, catch if any of them fails

Slide 18

Slide 18 text

race then after one of the promises returns, catch if any of them fails

Slide 19

Slide 19 text

var p3 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, "three"); }); var p4 = new Promise(function(resolve, reject) { setTimeout(reject, 500, "four"); }); Promise.race([p3, p4]).then(function(value) { console.log(value); //three }

Slide 20

Slide 20 text

example

Slide 21

Slide 21 text

const setStatus = (group, status) => { const data = { status: status, shopId: group.shop.id, }; return $http.post(`${API}/group/${group.id}/shop/status`, data); }; // service

Slide 22

Slide 22 text

ShopListService.setStatus(this.group, 'closed') .then( () => this.group.shop.status = 'closed'); // controller

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

further reading

Slide 25

Slide 25 text

articles

Slide 26

Slide 26 text

html5rocks http://www.html5rocks.com/en/tutorials/es6/promises nomadev http://nomadev.com.br/angularjs-promises-promessas-o-guia- definitivo/ david walsh https://davidwalsh.name/write-javascript-promises

Slide 27

Slide 27 text

libs

Slide 28

Slide 28 text

$q https://github.com/kriskowal/q bluebird http://bluebirdjs.com/

Slide 29

Slide 29 text

python sorocaba - https://groups.google.com/forum/m/#!forum/python-sorocaba guru sorocaba - https://www.facebook.com/GURUSorocaba/ hacklab - https://www.facebook.com/hacklabsorocaba/ GDG Sorocaba - https://www.facebook.com/GDGSorocaba/ SorocabaJS - https://www.facebook.com/sorocabajs/ JS Meetup - http://www.meetup.com/GDG-Sorocaba/ WomenTechmakers - https://www.facebook.com/groups/wtmsorocaba/ in.tegra.do - http://www.meetup.com/In-Tegra-Do/ english meetup - http://www.meetup.com/Sorocaba-English-Conversation-Happy-Hours/ #chegaMais

Slide 30

Slide 30 text

Slide 31

Slide 31 text

co-authored by felipe l. soares caique mitsuoka jean vitor

Slide 32

Slide 32 text

speakerdeck.com/felipebernardes valeu!