object-path icon indicating copy to clipboard operation
object-path copied to clipboard

Default value can be a callback or the fourth parameter of .get()

Open jvinai opened this issue 10 years ago • 9 comments

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.

jvinai avatar Feb 13 '15 15:02 jvinai

Hi, Could you please make a short example of how this should look like? On Feb 13, 2015 3:25 PM, "Jeremy Vinai" [email protected] wrote:

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.

— Reply to this email directly or view it on GitHub https://github.com/mariocasciaro/object-path/issues/30.

mariocasciaro avatar Feb 13 '15 15:02 mariocasciaro

like :

var obj = { 
foo: { 
  bar: { } 
}
}
var value = objectPath.get(obj, "foo.bar.test.nothing", "nop", function(err) {
  console.log("Error: ", err.message);
// Error: path "test" is undefined
});

value = "nop"

jvinai avatar Feb 13 '15 15:02 jvinai

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
});

pocesar avatar Feb 14 '15 03:02 pocesar

Why callback? This is not even async.

stevemao avatar Apr 17 '15 23:04 stevemao

https://www.joyent.com/developers/node/design/errors

stevemao avatar Apr 17 '15 23:04 stevemao

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
});

pocesar avatar Apr 18 '15 17:04 pocesar

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.

fs.readFile(filename[, options], callback) error is passed in callback if any. fs.readFileSync(filename[, options]) error is thrown if any. There is no callback.

stevemao avatar Apr 19 '15 00:04 stevemao

well, just like Array.prototype.forEach, the array can be walked using loops just fine, or be wrapped inside a generator and iterated.

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 file_get_contents comes to mind (reads an entire file to a variable), when doing procedural javascript was the norm, since there were no composable interfaces (like stream or promises), some people are still stuck with the callback mentality.

pocesar avatar Apr 19 '15 01:04 pocesar

This sounds like the same thing I wanted...an object that emits an event when it changes. I made this..... https://gist.github.com/PAEz/b6fc1687e4e963796f189d539d8d9d0c ....seems to do what I want. Didnt need error checking, but after reading this threw some in to check the path at least, could be good for debugging.

PAEz avatar Nov 01 '20 06:11 PAEz