Re:“var functionName” vs “function functionName”
by AdrianRossouw on Wednesday April 30, 2014 @09:16AM
(#7673030)
This is what I mean. The code that uses the callbacks
go first, the callbacks themselves are all named and
access to closures withstanding, not nested.
// this is all the code that is used,
var _changes = follow(couchOpts);
_changes.on('change', onChange);
_changes.on('error', onError);
_changes.resume();
// from here is just named functions
function onChange(data) {
_changes.pause();
save(data.seqId, saveFn);
}
function saveFn(err, entity) {
if (err) { return console.log(err); }
_changes.resume();
}
function onError(err) {
console.log(err);
}
Re:“var functionName” vs “function functionName”
by icebraining on Wednesday April 30, 2014 @08:06AM
(#7672734)
Why not do the reverse, and declare the inner functions before?
Re:“var functionName” vs “function functionName”
by AdrianRossouw on Wednesday April 30, 2014 @09:20AM
(#7673051)
coffeescript forces you to do that, and I do use it that way sometimes in CS.
I don't do it as much as i would with plain js, because the function definition feels less awkward, but I do still prefer named functions over anonymous ones. even if the name only exists in the local scope.
Re:“var functionName” vs “function functionName”
by wakaflaka on Wednesday April 30, 2014 @07:20AM
(#7672599)
You are just asking for race condition issues with that type of "solution."
Re:“var functionName” vs “function functionName”
by Jare on Wednesday April 30, 2014 @08:12AM
(#7672760)
Are you referring to a case like this?
{
f();
var a = 3;
function f() {
console.log(a);
}
}
That will print "undefined" because the entire function f() is hoisted and callable in the first line, but the variable a has not yet been assigned the value. That's not a race condition (those are related to timing in concurrent systems) but is certainly the sort of counter-intuitive visibility that makes me prefer to use function expressions most of the time.
Re:“var functionName” vs “function functionName”
by andrey-p on Wednesday April 30, 2014 @08:12AM
(#7672759)
You can still pass in callbacks to them. So if I got the parent's intent right, you've got:
function bunchOfAsyncThings(callback) {
var function1, function2;
function1 = function (callback) {
// do stuff here, then do callback
function2(callback);
};
function 2 = function (callback) {
// done with everything
callback();
};
function1(callback);
}
(Edit: formatting)
it's one of my main tools to fight the callback pyramid of doom.
use named functions, place them below the code that uses them.
all flat.