Understanding the Node.js Event Loop
nodesource.com
nodesource.com
IMHO, there's just a few things you need to understand to start getting productive with js fast, #1 on the list is to know how the event loop in the browser/node works and this is a really good intro to the node side of things. #2 is event bubbling/capturing in the DOM (http://javascript.info/tutorial/bubbling-and-capturing), and #3 would be everything about functions / function scoping.
Those are the parts I always try to explain first to devs I've mentored who were just jumping in to javascript.
However, there is nothing occult about software development and we shouldn't use "magic" as an excuse for not understanding or describing things thoroughly. Just imagine your doctor explaining you a surgery in these terms.
Otherwise, a very good and refreshing read.
Imagine trying to explain to someone how math operations work in js, you wouldn't really want to explain how numbers are represented in the computer until you get to something that necessitates that understanding - like bit operations.
So, I'd just say it depends on your goals, if your goal is to explain precisely how every detail of something works, then yes I'd agree with you, there's no excuse for magic. But if you want to explain how a general concept works I'm fine with loads of "magic" holes you can fill in for yourself later.
To describe something as magic in a serious context is toxic. We're not sleight of hand performers, we're engineers. It's more helpful to state something like, "this library is complicated and the internals are beyond the scope of this post". A pointer to documentation and source code on libuv would be even better. In face, this series of articles on libuv are a great start for those who are interested: https://nikhilm.github.io/uvbook/introduction.html
Also the 'magic box'.
The Urban Dictionary (the place where I look for colloquialisms) has this definition: "with or through complexity either spurios to the contemproary context or to long to be reasonable or desired of explanation". [1]
1: http://www.urbandictionary.com/define.php?term=magic&defid=9...
Until, of course, the final chapter (or was it the chapter before the final chapter), where you learned that magic isn't real.
http://stackoverflow.com/questions/25915634/difference-betwe... has a decent overview
var tick = Promise.resolve();
async function cpuBoundJob() {
for (...many iterations...) {
// crunch some bits
await tick;
}
}
...would spread out a heavy calculation and allow other things to break in occasionally and do work. But based on tests, nothing ever got a chance to run until the whole job finished.My current understanding (correct me if I'm wrong) is that promise.then() calls you back from a microtask, which means "await tick" doesn't teleport you into the next tick, just out of the current call stack into a new one at the end of the same tick, regardless of how many things are piled up waiting on the next tick. In other words the entire calculation remains one big CPU-blocking chunk. This modified version ended up working as expected, since setImmediate is (apparently) not a microtask.
var tick = () => new Promise(res => setImmediate(res))
async function cpuBoundJob() {
for (...many iterations...) {
// crunch some bits
await tick();
}
}
[1] https://promisesaplus.com/#point-34Putting the error first would enable Async or Promise libs to work...
I think it should be this.emit('fire')
And yet we've seen a proliferation of fibre/coroutine/green thread libraries for Node which all reinvent the concept of threading poorly.
> all reinvent the concept of threading poorly
because "user scripts" in node have no concept of threading to begin with. There is no concurrency in a node process, only non blocking I/O.