Talk i gave at WebTech Conference on November 10th 2010.
Abstract:
Why is web application programming so difficult? Is it javascript fault? Is it our fault? Time to take the red pill and wake up in the real, event driven world. A world where if you can dodge the bullets of skyrocketing complexity, your programs can be made scalable, fault tolerant, extensible and just beautiful.
Also
http://federico.galassi.net/
http://www.webtechcon.it
Follow me on Twitter!
https://twitter.com/federicogalassi
63. Callbacks
// callbacks give
// non linear execution
wally.takeJob(function work() ...)
wally.getCoffee(function drink() ...)
// ... later ...
// first drink coffee
// then work
64. Simple event loop
// make it look concurrent
button.onclick(function() {
...
})
UI update
Click handler
UI update
Click handler
Click handlerUI
event
queue
time
User click
65. Non-blocking I/O
// network async api
xhr.onreadystatechange = function(){
...
})
// DOM manipulations are synchronous
// but in memory so very fast
div.innerHTML = “Hello”
70. function breakfast() {
var bacon = bacon()
var juice = orangeJuice()
eat(bacon, juice)
}
Simple sequential
function bacon() {
// get bacon
return bacon
}
computation
71. function breakfast() {
var bacon = bacon()
var juice = orangeJuice()
eat(bacon, juice)
}
function bacon() {
getBacon(function(bacon) {
// got bacon
})
}
Async gets in
wrong
return what?
72. Break computation
function breakfast() {
var callback = function(bacon) {
var juice = getOrangeJuice()
eat(bacon, juice)
}
bacon(callback)
}
function bacon(callback) {
// get bacon async
callback(bacon)
}
rest of computation
computation
73. Break more
function breakfast() {
var callback = function(bacon) {
var callback = function(juice) {
eat(bacon, juice)
}
getOrangeJuice(callback)
}
bacon(callback)
}
rest of computation 1
computation
rest of computation 2
78. // not so simple sleep
setTimeout(function() {
doSomething()
}, 3000)
sleepit’s Hard
79. // simple sequential loop
images.forEach(function(url)
var image = fetchImage(url)
image.show()
}
loopit’s Hard
80. // fetchImage is async
images.forEach(function(url)
fetchImage(url, function(image) {
image.show()
})
}
loopit’s Hard
81. // Show them in the right order
function processImage() {
var url = images.shift()
if (url) {
fetchImage(url, function(image) {
image.show()
processImage()
})
}
}
processImage()
loopit’s Hard
95. Jeremy Ashkenas - CoffeeScript
“I don't think we want to take CoffeeScript down that
path. Open the Pandora's box of injecting special
functions into the runtime, and ... suddenly you have to
worry about being orders of magnitude slower than
normal JS.”
“Case in point, Stratified JS:A virtuoso performance of
JavaScript compilation, but look at what it compiles into.”
https://github.com/jashkenas/coffee-script/issuesearch?state=closed&q=asynchronous#issue/350/comment/330116
97. “I will be removing wait() in the next release of Node.
It has already been removed from the documentation.”
“A proper implementation of wait() necessitates true
coroutines”
“This sort of mental complication is exactly what I'm
trying to avoid in Node.”
Ryan Dahl - node.js
http://groups.google.com/group/nodejs/msg/df199d233ff17efa