|
|
|
|
|
by MatthewPhillips
5225 days ago
|
|
You have to think about async code differently than you think about synchronous code. In PHP you might write: $data = getData();
And people want to substitute the return for a callback in js. getData(function(data) {
// Do something with data.
});
That indeed does lead to callback mess. But when you are coding in js you rarely should use anonymous functions. I mostly just use them when I might want to perform recursion. Going back to my example, I would write the operation as an object like so: var SomeObject = {
start: function() {
getData(dataGot);
},
dataGot: function(data) {
}
};
JavaScript is an object-oriented languages; use objects! They make async painless. |
|
I'd still end up with this, thanks to callbacks.
Seems to me that what's missing is something that lets me write it in a more simple way. What I have here looks like an absolute nightmare of coupling. So if you have some suggestions you could point me to that show a little more detail, I'd be really grateful.