practical promises
play

Practical Promises As opposed to impractical promises 1 what is - PowerPoint PPT Presentation

Practical Promises As opposed to impractical promises 1 what is asynchronous code? Asynchronous (aka async ) just means: takes some time or happens in the future, not right now and JavaScript won't wait for it. 3 what is


  1. Practical Promises As opposed to impractical promises 1

  2. what is asynchronous code? Asynchronous (aka async ) just means: “takes some time” or “happens in the future, not right now”… …and JavaScript won't wait for it. 3

  3. what is asynchronous code? console.log("One") setTimeout(() => console.log("Two"), 10) console.log("Three") ๏ In which order will the logs fire? 4

  4. what is a asynchronous code? console.log("One") setTimeout(() => console.log("Two"), 10) console.log("Three") ๏ In which order will the logs fire? One Three Two 5

  5. what is asynchronous code? File System Your Event Code Database Loop Network 6

  6. How do we handle asynchronous code? 1. Callbacks 7

  7. what is a callback? 8

  8. what is a callback? Technically: a function passed to another function • Blocking two flavors… • Non-blocking 9

  9. Async with Callbacks console.log("Getting Configuration") fs.readFile('/config.json', 'utf8', (err, data) => { console.log("Got configuration:", data) }); console.log("Moving on…"); ๏ BTW, In which order will the logs fire? 10

  10. Problems with Callbacks const tryGetRich = () => { readFile('/luckyNumbers.txt', (err, fileContent) => { // Do something with lucky numbers }) } 11

  11. Problems with Callbacks const tryGetRich = () => { readFile('/luckyNumbers.txt', (err, fileContent) => { nums = fileContent.split(","); nums.forEach(num => { bookmaker.getHorse(num, (err, horse) => { // Ok, this is getting a little confusing }) }) }) } 12

  12. Problems with Callbacks const tryGetRich = () => { readFile('/luckyNumbers.txt', (err, fileContent) => { nums = fileContent.split(","); nums.forEach(num => { bookmaker.getHorse(num, (err, horse) => { bookmaker.bet(horse, (err, success) => { if(success) { // Help... } }) }) console.log('When will I run??') }) }) } 13

  13. like this? var result; setTimeout( function cb () { result = 'hello'; }, 0); console.log(result); 14

  14. like this? var result = setTimeout( function cb () { return 'hello'; }, 0); console.log(result); 15

  15. like this? setTimeout( function cb () { var result = 'hello'; console.log(result); }, 0); 16

  16. How do we handle asynchronous code? 1. Callbacks 2. Promises 17

  17. promise “A promise represents the eventual result 
 of an asynchronous operation.” — The Promises/A+ Spec 18

  18. vanilla async callback 
 fs.readFile(‘file.txt’, function callback (err, data) {…} ); Callback v promise async promise 
 fs.readFileAsync(‘file.txt’) 
 .then( function onSuccess (data) {…}, function onError (err) {…} ); 19

  19. promise { readFileAsync(‘/luckyNumber.txt’) [[PromiseValue]]: undefined, [[PromiseStatus]]: "pending" } 20

  20. promise { readFileAsync(‘/luckyNumber.txt’) [[PromiseValue]]: "42", [[PromiseStatus]]: "fulfilled" } 21

  21. like this? var result; promisifiedSetTimeout(0) .then( function success () { result = 'hello'; }); console.log(result); 22

  22. like this? var result = promisifiedSetTimeout(0) .then( function success () { return 'hello'; }); console.log(result); 23

  23. like this? promisifiedSetTimeout(0) .then( function success () { var result = 'hello'; console.log(result); }); 24

  24. reading a file synchronous async (callbacks) async var path = ‘demo-poem.txt'; var path = 'demo-poem.txt'; var path = 'demo-poem.txt'; console.log('- I am first -'); fs.readFile(path, function (err, buff) { promisifiedReadFile(path) if (err) console.error(err); .then( function (buff) { try { else console.log(buff.toString()); console.log(buff.toString()); var buff = fs.readFileSync(path); console.log('- I am last -'); }, function (err) { console.log(buff.toString()); }); console.error(err); } catch (err) { }) console.error(err); console.log('- I am first -'); .then( function () { } console.log('- I am last -'); }); console.log('- I am last -'); console.log('- I am first -'); 25

  25. error handling fine better best* var path = 'demo-poem.txt'; var path = ‘demo-poem.txt'; var path = ‘demo-poem.txt'; promisifiedReadFile(path) promisifiedReadFile(path) .then( function (buff) { .then( function (buff) { promisifiedReadFile(path) console.log(buff.toString()); console.log(buff.toString()); .then( function (buff) { }, function (err) { }) console.log(buff.toString()); console.error(err); .catch( function (err) { }) }); console.error(err); .then(null, function (err) { }); console.error(err); }); 26 26

  26. error handling continued var path = ‘demo-poem.txt’; var path = ‘demo-poem-2.txt’; promisifiedReadFile(path) .then( function (buff) { console.log(buff.toString()); return promisifiedReadFile(path2); }) .then( function (buff) { console.log(buff.toString()); }) .catch( function (err) { console.error(err); }); 27

  27. promise advantages • Portable • Multiple handlers • “Linear” • Unified error handling 28

Recommend


More recommend