SlideShare a Scribd company logo
1 of 44
#dotbari
Giuseppe Santoro 
ApuliaSoft
Gestire l'asincronia in 
JavaScript 
uno sguardo al futuro!
C’era una volta
$conn = $db.connect(‘localhost:1234’); 
$user = $conn.query(‘SELECT . . .’); 
$user.name = ‘Pippo’; 
$user.save(); 
echo(‘salvato’);
Apache Server 
t1 
Richiesta Creazione di 1 thread
Apache Server 
t1 
t2 
2 richieste Creazione di 2 threads
Apache Server 
t1 
t2 
t3 
t4 
4 richieste Creazione di 4 threads
$conn = $db.connect(‘localhost:1234’); 
$user = $conn.query(‘SELECT . . .’); 
$user.name = ‘Pippo’; 
$user.save(); 
echo(‘salvato’);
$conn = $db.connect(‘localhost:1234’); 
$user = $conn.query(‘SELECT . . .’); 
$user.name = ‘Pippo’; 
$user.save(); 
echo(‘salvato’);
var conn = db.connect(‘localhost:1234’); 
var user = conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
user.save(); 
console.log(‘salvato’);
var conn = db.connect(‘localhost:1234’); 
var user = conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
user.save(); 
console.log(‘salvato’);
var conn = db.connect(‘localhost:1234’); 
var user = conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
user.save(); 
console.log(‘salvato’);
var conn = db.connect(‘localhost:1234’); 
var user = conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
user.save(); 
console.log(‘salvato’);
var conn = db.connect(‘localhost:1234’); 
var user = conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
user.save(); 
console.log(‘salvato’);
t1 
1 richiesta 1 thread
t1 
2 richieste 1 thread
t1 
4 richieste 1 thread
t1 
4 richieste 1 thread
Node.js
var conn = db.connect(‘localhost:1234’); 
var user = conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
user.save(); 
console.log(‘salvato’);
‘il passato’: CALLBACK
var conn = db.connect(‘localhost:1234’);
var conn = db.connect(‘localhost:1234’); 
db.connect(‘localhost:1234’, function(err,conn){ 
// async code 
}); 
// code code code code
db.connect(‘localhost:1234’, function(err,conn){ 
conn.query(‘SELECT...’, function(err,user){ 
user.name = ‘Pippo’; 
user.save(function(err){ 
console.log(‘salvato’); 
}); 
}); 
});
db.connect(‘localhost:1234’, function(err,conn){ 
if (err) return console.log(‘errore’); 
conn.query(‘SELECT...’, function(err,user){ 
if (err) return console.log(‘errore’); 
user.name = ‘Pippo’; 
user.save(function(err){ 
if (err) return console.log(‘errore’); 
console.log(‘salvato’); 
}); 
}); 
});
‘il presente’: PROMISE
var conn = db.connect(‘localhost:1234’);
var conn = db.connect(‘localhost:1234’); 
var promisedConn = db.connect(‘localhost:1234’); 
// code code code code 
promisedConn.then(function(conn){ 
// async code 
}); 
// code code code code
var conn = db.connect(‘localhost:1234’); 
var promisedConn = db.connect(‘localhost:1234’); 
// code code code code 
promisedConn.then(function(conn){ 
// async code 
}).then(function(val){}); 
// code code code code
IDEA: chaining
db.connect(‘localhost:1234’) 
.then(function(conn){ 
return conn.query(‘SELECT...’); 
}).then(function(user){ 
user.name = ‘Pippo’; 
return user.save(); 
}).then(function(){ 
console.log(‘salvato’); 
});
db.connect(‘localhost:1234’) 
.then(function(conn){ 
return conn.query(‘SELECT...’); 
}).then(function(user){ 
user.name = ‘Pippo’; 
return user.save(); 
}).then(function(){ 
console.log(‘salvato’); 
}).fail(function(){ 
console.log(‘errore’); 
});
‘il futuro’: GENERATOR
yield
function count(n){ 
var res = [] 
for (var x = 0; x < n; x++) { 
res.push(x) 
} 
return res 
} 
for (var x of count(5)) { 
console.log(x) 
}
function* count(n){ 
for (var x = 0; x < n; x++) { 
yield x 
} 
} 
for (var x of count(5)) { 
console.log(x) 
}
co(function *(){ 
var a = yield get('http://google.com'); 
var b = yield get('http://yahoo.com'); 
})() 
co(function *(){ 
var c = yield get('http://apple.com'); 
var d = yield get('http://microsoft.com'); 
})()
var conn = db.connect(‘localhost:1234’); 
var user = conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
user.save(); 
console.log(‘salvato’);
co(function *(){ 
var conn = yield db.connect(‘localhost:1234’); 
var user = yield conn.query(‘SELECT . . .’); 
user.name = ‘Pippo’; 
yield user.save(); 
console.log(‘salvato’); 
})();
Compatibilita’ 
0.11.131 
$ node -harmony [filename]
Compatibilita’
Compatibilita’
Grazie per l’ascolto 
Domande ? ?

More Related Content

What's hot

Java script.trend(spec)
Java script.trend(spec)Java script.trend(spec)
Java script.trend(spec)dynamis
 
Simular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentariaSimular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentariajbersosa
 
Создание новых объектов
Создание новых объектовСоздание новых объектов
Создание новых объектовConstantin Kichinsky
 
Javascript and jQuery for Mobile
Javascript and jQuery for MobileJavascript and jQuery for Mobile
Javascript and jQuery for MobileIvano Malavolta
 
Sumahexavector
SumahexavectorSumahexavector
Sumahexavectorjbersosa
 
Clase 7 el modelo
Clase 7  el modeloClase 7  el modelo
Clase 7 el modelohydras_cs
 
Java Script - Object-Oriented Programming
Java Script - Object-Oriented ProgrammingJava Script - Object-Oriented Programming
Java Script - Object-Oriented Programmingintive
 
Primeiros passo com Docker - DevParaná
Primeiros passo com Docker - DevParanáPrimeiros passo com Docker - DevParaná
Primeiros passo com Docker - DevParanáRenan Martins Pimentel
 
Modern Mobile Web Apps
Modern Mobile Web AppsModern Mobile Web Apps
Modern Mobile Web Appsdynamis
 
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tipsOpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tipsHo Kim
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometrojubacalo
 
Pimp your site with jQuery!
Pimp your site with jQuery!Pimp your site with jQuery!
Pimp your site with jQuery!Elliott Kember
 
RubyからJavaScriptへ (!tDiary会議 2011)
RubyからJavaScriptへ (!tDiary会議 2011)RubyからJavaScriptへ (!tDiary会議 2011)
RubyからJavaScriptへ (!tDiary会議 2011)Kohei MATSUOKA
 

What's hot (20)

Java script.trend(spec)
Java script.trend(spec)Java script.trend(spec)
Java script.trend(spec)
 
Simular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentariaSimular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentaria
 
Silex al límite
Silex al límiteSilex al límite
Silex al límite
 
Создание новых объектов
Создание новых объектовСоздание новых объектов
Создание новых объектов
 
Javascript and jQuery for Mobile
Javascript and jQuery for MobileJavascript and jQuery for Mobile
Javascript and jQuery for Mobile
 
Socket.io - Intro
Socket.io - IntroSocket.io - Intro
Socket.io - Intro
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
Primeiros passos-com-docker
Primeiros passos-com-dockerPrimeiros passos-com-docker
Primeiros passos-com-docker
 
Sumahexavector
SumahexavectorSumahexavector
Sumahexavector
 
Clase 7 el modelo
Clase 7  el modeloClase 7  el modelo
Clase 7 el modelo
 
Java Script - Object-Oriented Programming
Java Script - Object-Oriented ProgrammingJava Script - Object-Oriented Programming
Java Script - Object-Oriented Programming
 
Sumahex
SumahexSumahex
Sumahex
 
Dart != JavaScript
Dart != JavaScriptDart != JavaScript
Dart != JavaScript
 
Primeiros passo com Docker - DevParaná
Primeiros passo com Docker - DevParanáPrimeiros passo com Docker - DevParaná
Primeiros passo com Docker - DevParaná
 
Modern Mobile Web Apps
Modern Mobile Web AppsModern Mobile Web Apps
Modern Mobile Web Apps
 
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tipsOpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
 
Dart und JavaScript
Dart und JavaScriptDart und JavaScript
Dart und JavaScript
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometro
 
Pimp your site with jQuery!
Pimp your site with jQuery!Pimp your site with jQuery!
Pimp your site with jQuery!
 
RubyからJavaScriptへ (!tDiary会議 2011)
RubyからJavaScriptへ (!tDiary会議 2011)RubyからJavaScriptへ (!tDiary会議 2011)
RubyからJavaScriptへ (!tDiary会議 2011)
 

Viewers also liked

07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...
07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...
07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...Mirza Musić
 
Sviluppo web con Yii
Sviluppo web con YiiSviluppo web con Yii
Sviluppo web con YiiApuliaSoft
 
Gems You Shouldn't Leave Development Without
Gems You Shouldn't Leave Development WithoutGems You Shouldn't Leave Development Without
Gems You Shouldn't Leave Development WithoutDavid Underwood
 
Deploying Your Webapps (with Capistrano)
Deploying Your Webapps (with Capistrano)Deploying Your Webapps (with Capistrano)
Deploying Your Webapps (with Capistrano)David Underwood
 
Periscope a New Technology for Network Marketing Leaders
Periscope a New Technology for Network Marketing LeadersPeriscope a New Technology for Network Marketing Leaders
Periscope a New Technology for Network Marketing LeadersDale Calvert
 
Integrated and sustainable water management of Red-Thai Binh rivers system un...
Integrated and sustainable water management of Red-Thai Binh rivers system un...Integrated and sustainable water management of Red-Thai Binh rivers system un...
Integrated and sustainable water management of Red-Thai Binh rivers system un...Environmental Intelligence Lab
 
A FOSS approach to Integrated Water Resource Management
A FOSS approach to Integrated Water Resource ManagementA FOSS approach to Integrated Water Resource Management
A FOSS approach to Integrated Water Resource ManagementEnvironmental Intelligence Lab
 
La Mucca Viola
La Mucca ViolaLa Mucca Viola
La Mucca ViolaApuliaSoft
 
Improving hydropower reservoir operation by using snow-related information
Improving hydropower reservoir operation by using snow-related informationImproving hydropower reservoir operation by using snow-related information
Improving hydropower reservoir operation by using snow-related informationEnvironmental Intelligence Lab
 
Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...
Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...
Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...Environmental Intelligence Lab
 
Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité
Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité
Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité Caroline VLAEMINCK
 
La publicité sur les réseaux sociaux
La publicité sur les réseaux sociauxLa publicité sur les réseaux sociaux
La publicité sur les réseaux sociauxCaroline VLAEMINCK
 
Week2 innoculationprocess
Week2 innoculationprocessWeek2 innoculationprocess
Week2 innoculationprocessDale Calvert
 
Suez Environnement - Guide medias sociaux
Suez Environnement - Guide medias sociauxSuez Environnement - Guide medias sociaux
Suez Environnement - Guide medias sociauxCaroline VLAEMINCK
 

Viewers also liked (15)

Geoportale sistema Maggiore-Lugano
Geoportale sistema Maggiore-LuganoGeoportale sistema Maggiore-Lugano
Geoportale sistema Maggiore-Lugano
 
07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...
07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...
07 postupci povecanja_termickog_stupnja_djelovanja_u_termoelektranama_s_parni...
 
Sviluppo web con Yii
Sviluppo web con YiiSviluppo web con Yii
Sviluppo web con Yii
 
Gems You Shouldn't Leave Development Without
Gems You Shouldn't Leave Development WithoutGems You Shouldn't Leave Development Without
Gems You Shouldn't Leave Development Without
 
Deploying Your Webapps (with Capistrano)
Deploying Your Webapps (with Capistrano)Deploying Your Webapps (with Capistrano)
Deploying Your Webapps (with Capistrano)
 
Periscope a New Technology for Network Marketing Leaders
Periscope a New Technology for Network Marketing LeadersPeriscope a New Technology for Network Marketing Leaders
Periscope a New Technology for Network Marketing Leaders
 
Integrated and sustainable water management of Red-Thai Binh rivers system un...
Integrated and sustainable water management of Red-Thai Binh rivers system un...Integrated and sustainable water management of Red-Thai Binh rivers system un...
Integrated and sustainable water management of Red-Thai Binh rivers system un...
 
A FOSS approach to Integrated Water Resource Management
A FOSS approach to Integrated Water Resource ManagementA FOSS approach to Integrated Water Resource Management
A FOSS approach to Integrated Water Resource Management
 
La Mucca Viola
La Mucca ViolaLa Mucca Viola
La Mucca Viola
 
Improving hydropower reservoir operation by using snow-related information
Improving hydropower reservoir operation by using snow-related informationImproving hydropower reservoir operation by using snow-related information
Improving hydropower reservoir operation by using snow-related information
 
Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...
Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...
Curses, tradeoffs, and scalable management: advancing evolutionary direct pol...
 
Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité
Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité
Caroline VLAEMINCK - Comment mesurer les indicateurs de suivi et d’efficacité
 
La publicité sur les réseaux sociaux
La publicité sur les réseaux sociauxLa publicité sur les réseaux sociaux
La publicité sur les réseaux sociaux
 
Week2 innoculationprocess
Week2 innoculationprocessWeek2 innoculationprocess
Week2 innoculationprocess
 
Suez Environnement - Guide medias sociaux
Suez Environnement - Guide medias sociauxSuez Environnement - Guide medias sociaux
Suez Environnement - Guide medias sociaux
 

Gestire l'asincronia in javascript uno sguardo al futuro!