Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
I looked around for a decent explanation of JavaScript callbacks, but they take too long to get to the point, or they never explain what they are. Maybe it's too hard to explain? I've been writing a lot of docs lately, so let me try! :-)
Callbacks are used when an operation is going to take a long time and your program wants to do other things while it's waiting. So you create a function to run when the operation is done, and provide that function to the slow operation you're calling with the intent that it will call this function when it's done. Thus it is a callback function.
An example:
If you look in the console, you'll see:
An example of a real world callback.
After you upload a video to Facebook, you'll get a dialog when the upload is done saying this is going to take a bit of time to process, so we'll let you know when it's done. In the meantime you can read stuff. When the video is ready, you get a message saying you can watch the video now. That's the basic flow of a callback.
Following up on yesterday's news about Drummer, the key innovation is that you can write JavaScript code that's much simpler, because it does not need to use callbacks.
An example:
dns.getDomainName (dns.getDottedId (liveblog.co))This works even though both DNS operations do I/O.
We are using JavaScript's threading, we are not blocking the CPU.
A programmer in any other language will be scratching their head wondering what's the big deal. I know. I've been saying that for years. If Python can do this, I don't see why JavaScript shouldn't.
Note: JavaScript as spec'd can't do this. But we were able to make this work with a very simple pre-processor.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.
Read more of this story at Slashdot.