O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
ASYNC ALL 
AROUND US 
FRANCISCO MS FERREIRA 
@fmsf303
JS 
Promises
Java 
Completable 
Futures 
JS 
Promises
Dart 
Futures 
Java 
Completable 
Futures 
JS 
Promises
Async 
COMMON 
PROBLEM
MY CODE DOESN’T WORK 
exports.saveWeekAvailability = function(req, res){ 
console.log("-- SAVE WEEK AVAILABILITY --"); 
we...
MY CODE DOESN’T WORK 
resultArr[i] = Availability(weekData[i]).save(function(err, result){ 
return result; 
});
MY CODE DOESN’T WORK 
router.get('/about', function(req, res) { 
var animalsData = sequelize.query("SELECT * FROM animals"...
PYRAMID OF DOOM 
router.get('/about', function(req, res) { 
sequelize.query("SELECT * FROM animals").success( function( an...
JAVA 
public CompletableFuture<String> ask() { 
final CompletableFuture<String> future = new CompletableFuture<>(); 
//......
JAVA 
public CompletableFuture<String> ask() { 
final CompletableFuture<String> future = new CompletableFuture<>(); 
//......
JAVA 
public CompletableFuture<String> ask() { 
final CompletableFuture<String> future = new CompletableFuture<>(); 
//......
DART 
void printDailyNewsDigest() { 
File file = new File("dailyNewsDigest.txt"); 
Future future = file.readAsString(); 
f...
DART 
Future<Item> getItem(){ 
var completer = new Completer(); 
// At some time you need to complete the future: 
complet...
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
PROMISES 
this.updateEmailsInDatabase = function() { 
var fetchEmailDeferer = Q.defer(); 
inboxHandler 
.listMessages() 
....
WHAT IS A PROMISE? 
- Like a future 
- I promise that I will resolve a value
WHAT IS A PROMISE? 
- Like a future 
- I promise that I will resolve a value 
- function fooBar() { 
- …; 
- return promis...
WHAT IS A PROMISE? 
- Like a future 
- I promise that I will resolve a value 
- function fooBar() { 
- var deferer = Q.def...
WHAT IS A PROMISE? 
- Like a future 
- I promise that I will resolve a value 
- function fooBar() { 
- var deferer = Q.def...
WHAT IS A PROMISE? 
- Like a future 
- I promise that I will resolve a value 
- function fooBar() { 
- var deferer = Q.def...
ARGUMENTS
ARGUMENT FLOW 
inboxHandler 
.listMessages() 
.then( saveMessagesFromEmailServerLocally ) 
.then( fetchEmailDeferer.resolv...
ARGUMENT FLOW 
inboxHandler 
.listMessages() 
.then( saveMessagesFromEmailServerLocally ) 
.then( fetchEmailDeferer.resolv...
BI-DIRECTIONAL 
ARGUMENTS
BI-DIRECTIONAL 
ARGUMENTS 
databasePromise 
.then( openCollection( dbconfig.getUsersCollectionName() ) ) 
.then( insert(us...
BI-DIRECTIONAL 
ARGUMENTS 
databasePromise 
.then( openCollection( dbconfig.getUsersCollectionName() ) ) 
.then( insert(us...
BI-DIRECTIONAL 
ARGUMENTS 
//.then( openCollection( dbconfig.getUsersCollectionName() ) ) 
function openCollection( colect...
BI-DIRECTIONAL 
ARGUMENTS 
//.then( openCollection( dbconfig.getUsersCollectionName() ) ) 
function openCollection( colect...
BI-DIRECTIONAL 
ARGUMENTS 
//.then( openCollection( dbconfig.getUsersCollectionName() ) ) 
function openCollection( colect...
TEST YOUR STUFF!
PLEASE 
TEST YOUR STUFF!
UNIT TESTS IMPACT IN JS 
inboxHandler 
.listMessages() 
.then( saveMessagesFromEmailServerLocally ) 
it('should be able to...
READABLE & MAINTAINABLE 
inboxHandler 
.listMessages() 
.then( saveMessagesFromEmailServerLocally ) 
.then( fetchEmailDefe...
QUESTIONS 
http://j.rfer.us/EBRJimk0X
Async all around us (promises)
Async all around us (promises)
Async all around us (promises)
Async all around us (promises)
Próximos SlideShares
Carregando em…5
×

de

Async all around us (promises) Slide 1 Async all around us (promises) Slide 2 Async all around us (promises) Slide 3 Async all around us (promises) Slide 4 Async all around us (promises) Slide 5 Async all around us (promises) Slide 6 Async all around us (promises) Slide 7 Async all around us (promises) Slide 8 Async all around us (promises) Slide 9 Async all around us (promises) Slide 10 Async all around us (promises) Slide 11 Async all around us (promises) Slide 12 Async all around us (promises) Slide 13 Async all around us (promises) Slide 14 Async all around us (promises) Slide 15 Async all around us (promises) Slide 16 Async all around us (promises) Slide 17 Async all around us (promises) Slide 18 Async all around us (promises) Slide 19 Async all around us (promises) Slide 20 Async all around us (promises) Slide 21 Async all around us (promises) Slide 22 Async all around us (promises) Slide 23 Async all around us (promises) Slide 24 Async all around us (promises) Slide 25 Async all around us (promises) Slide 26 Async all around us (promises) Slide 27 Async all around us (promises) Slide 28 Async all around us (promises) Slide 29 Async all around us (promises) Slide 30 Async all around us (promises) Slide 31 Async all around us (promises) Slide 32 Async all around us (promises) Slide 33 Async all around us (promises) Slide 34 Async all around us (promises) Slide 35 Async all around us (promises) Slide 36 Async all around us (promises) Slide 37 Async all around us (promises) Slide 38 Async all around us (promises) Slide 39 Async all around us (promises) Slide 40 Async all around us (promises) Slide 41 Async all around us (promises) Slide 42 Async all around us (promises) Slide 43 Async all around us (promises) Slide 44 Async all around us (promises) Slide 45
Próximos SlideShares
Understanding Asynchronous JavaScript
Avançar
Transfira para ler offline e ver em ecrã inteiro.

0 gostaram

Compartilhar

Baixar para ler offline

Async all around us (promises)

Baixar para ler offline

25 min tech talk around javascript promises and futures

  • Seja a primeira pessoa a gostar disto

Async all around us (promises)

  1. 1. ASYNC ALL AROUND US FRANCISCO MS FERREIRA @fmsf303
  2. 2. JS Promises
  3. 3. Java Completable Futures JS Promises
  4. 4. Dart Futures Java Completable Futures JS Promises
  5. 5. Async COMMON PROBLEM
  6. 6. MY CODE DOESN’T WORK exports.saveWeekAvailability = function(req, res){ console.log("-- SAVE WEEK AVAILABILITY --"); weekData = req.body; var resultArr = []; for (var i=0; i< weekData.length; i++) { resultArr[i] = Availability(weekData[i]).save(function(err, result){ return result; }); } console.log("-- Result Arr --"); console.log(resultArr); };
  7. 7. MY CODE DOESN’T WORK resultArr[i] = Availability(weekData[i]).save(function(err, result){ return result; });
  8. 8. MY CODE DOESN’T WORK router.get('/about', function(req, res) { var animalsData = sequelize.query("SELECT * FROM animals").success(function(rows) { return rows; }); console.log(animals); // returns 'undefined' var humansData = sequelize.query("SELECT * FROM humans").success(function(rows) { return rows; }); console.log(humans); // returns 'undefined' res.render('about', { animals: animalsData, humans: humansData }); });
  9. 9. PYRAMID OF DOOM router.get('/about', function(req, res) { sequelize.query("SELECT * FROM animals").success( function( animals ) { sequelize.query("SELECT * FROM humans").success( function( humans ) { res.render('about', { animals: animalsData, humans: humansData }); }); }); });
  10. 10. JAVA public CompletableFuture<String> ask() { final CompletableFuture<String> future = new CompletableFuture<>(); //... return future; }
  11. 11. JAVA public CompletableFuture<String> ask() { final CompletableFuture<String> future = new CompletableFuture<>(); //... return future; } future.complete("42");
  12. 12. JAVA public CompletableFuture<String> ask() { final CompletableFuture<String> future = new CompletableFuture<>(); //... return future; } future.complete("42"); future.get(); // this will block execution
  13. 13. DART void printDailyNewsDigest() { File file = new File("dailyNewsDigest.txt"); Future future = file.readAsString(); future.then((content) { print(content); }); }
  14. 14. DART Future<Item> getItem(){ var completer = new Completer(); // At some time you need to complete the future: completer.complete(new Item()); return completer.future; }
  15. 15. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  16. 16. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  17. 17. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  18. 18. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  19. 19. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  20. 20. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  21. 21. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  22. 22. PROMISES this.updateEmailsInDatabase = function() { var fetchEmailDeferer = Q.defer(); inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); return fetchEmailDeferer.promise; };
  23. 23. WHAT IS A PROMISE? - Like a future - I promise that I will resolve a value
  24. 24. WHAT IS A PROMISE? - Like a future - I promise that I will resolve a value - function fooBar() { - …; - return promise; - }
  25. 25. WHAT IS A PROMISE? - Like a future - I promise that I will resolve a value - function fooBar() { - var deferer = Q.defer() - functionWithACallback( function( aValue) { - deferer.resolve( aValue ); - } - return defer.promise; - } - var promise = fooBar();
  26. 26. WHAT IS A PROMISE? - Like a future - I promise that I will resolve a value - function fooBar() { - var deferer = Q.defer() - functionWithACallback( function( aValue) { - deferer.resolve( aValue ); - } - return defer.promise; - } - var promise = fooBar();
  27. 27. WHAT IS A PROMISE? - Like a future - I promise that I will resolve a value - function fooBar() { - var deferer = Q.defer() - functionWithACallback( function( aValue) { - deferer.resolve( aValue ); - } - return defer.promise; - } - var promise = fooBar(); - promise.then( function( value ) { - console.log( “myValue:”, value ); - });
  28. 28. ARGUMENTS
  29. 29. ARGUMENT FLOW inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); messages listMessages saveMessagesFromEmailServerLocally
  30. 30. ARGUMENT FLOW inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done(); listMessages saveMessagesFromEmailServerLocally fetchEmailDeferer.resolve messages success
  31. 31. BI-DIRECTIONAL ARGUMENTS
  32. 32. BI-DIRECTIONAL ARGUMENTS databasePromise .then( openCollection( dbconfig.getUsersCollectionName() ) ) .then( insert(user) ) .then( addUserDeferer.resolve ) .done();
  33. 33. BI-DIRECTIONAL ARGUMENTS databasePromise .then( openCollection( dbconfig.getUsersCollectionName() ) ) .then( insert(user) ) .then( addUserDeferer.resolve ) .done(); databasePromise openCollection getCollection database db collection
  34. 34. BI-DIRECTIONAL ARGUMENTS //.then( openCollection( dbconfig.getUsersCollectionName() ) ) function openCollection( colectionToOpen ) { return function(db) { return db.collection( colectionToOpen ); }; } databasePromise openCollection getCollection database db collection
  35. 35. BI-DIRECTIONAL ARGUMENTS //.then( openCollection( dbconfig.getUsersCollectionName() ) ) function openCollection( colectionToOpen ) { return function(db) { return db.collection( colectionToOpen ); }; } databasePromise openCollection getCollection database db collection
  36. 36. BI-DIRECTIONAL ARGUMENTS //.then( openCollection( dbconfig.getUsersCollectionName() ) ) function openCollection( colectionToOpen ) { return function(db) { return db.collection( colectionToOpen ); }; } databasePromise openCollection getCollection database db collection
  37. 37. TEST YOUR STUFF!
  38. 38. PLEASE TEST YOUR STUFF!
  39. 39. UNIT TESTS IMPACT IN JS inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) it('should be able to list messages', function( done ) { inboxHandler.listMessages( 2 ).then( function( messages ) { messages.should.have.length( 2 ); }).done( done ); });
  40. 40. READABLE & MAINTAINABLE inboxHandler .listMessages() .then( saveMessagesFromEmailServerLocally ) .then( fetchEmailDeferer.resolve ) .done();
  41. 41. QUESTIONS http://j.rfer.us/EBRJimk0X

25 min tech talk around javascript promises and futures

Vistos

Vistos totais

1.034

No Slideshare

0

De incorporações

0

Número de incorporações

8

Ações

Baixados

1

Compartilhados

0

Comentários

0

Curtir

0

×