-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default value can be a callback or the fourth parameter of .get() #30
Comments
Hi,
|
like :
value = "nop" |
whats the problem of wrapping it in your own method? I think that's out of the scope of this library (callbacks, events and such) objectPath.cget = function(obj, path, cb) {
var val = objectPath.get(obj, path);
if (val === undefined){
cb(new Error("Path '" + path + "' is undefined"));
} else {
cb(null, val);
}
};
objectPath.cget(obj, "foo.bar.test.nothing", function(err, val) {
if (err) {
throw err;
}
// do something with val
}); |
Why callback? This is not even async. |
well, not everything that uses a callback is "async" (most Array prototype functions aren't, like sort, map, forEach, etc), they "block" further code execution and if they throw, they break the current cycle. real async code schedules for the next cycle, either with "hacking" using setTimeout with 0, setImmediate, requestAnimationFrame or process.nextTick. these two behave differently: var status = 0;
function cb(fn, async){
if (async) {
setImmediate(function(){
status++;
fn();
});
} else {
fn();
}
}
cb(function(){
console.log(status); // will print later and will be 1
}, true);
cb(function(){
console.log(status); // will print first, and will be 0
}); |
This is probably not the best example. Those array functions have to use a callback because there's no other way to do it. I think sync functions should avoid callback.
|
well, just like I/O operations should never be synchronous, node has those "sync" functions because when the callback was the default way to do things in node land, people couldn't wrap their head around their other languages background, for example PHP when |
This sounds like the same thing I wanted...an object that emits an event when it changes. |
I need to emit an event when the object is malformed.
Now i'm using a specific default value and i test if this value and emit the event.
For the callback, it can be nice if the first the parameter is an error with the current attribute name undefined or a string of this attribute name.
I can do a PR if you need.
The text was updated successfully, but these errors were encountered: