Everything in nodejs is non-blocking which is nice, but how would I go about making function alls that have to be one after the other without having a huge nested list of callbacks?
You don't have to nest your callbacks.
There are many patterns in writing asynchronous code.
For instance, this matrioska-nested-style...
database.find('foo', function (err, data) {
database.update('foo', 'bar', function (err, data) {
database.delete('bar', function (err, data) {
console.log(data);
});
});
});
... can be rewritten in a cleaner (but more verbose) way:
var onDelete = function (err, data) {
console.log(data);
},
onUpdate = function (err, data) {
database.delete('bar', onDelete);
},
onFind = function (err, data) {
database.update('foo', 'bar', onUpdate);
};
database.find('foo', onFind);
Another option is using a module to abstract serial and parallel execution of callbacks.
Use Step.
It's "a simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With