Skip to content
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

Open
jvinai opened this issue Feb 13, 2015 · 9 comments
Open

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

jvinai opened this issue Feb 13, 2015 · 9 comments
Labels
Milestone

Comments

@jvinai
Copy link

jvinai commented Feb 13, 2015

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.

@mariocasciaro
Copy link
Owner

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
#30.

@jvinai
Copy link
Author

jvinai commented Feb 13, 2015

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"

@pocesar
Copy link
Collaborator

pocesar commented Feb 14, 2015

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

@stevemao
Copy link
Contributor

Why callback? This is not even async.

@stevemao
Copy link
Contributor

@pocesar
Copy link
Collaborator

pocesar commented Apr 18, 2015

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

@stevemao
Copy link
Contributor

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.

@pocesar
Copy link
Collaborator

pocesar commented Apr 19, 2015

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 pocesar modified the milestone: 1.0.0 May 12, 2015
@pocesar pocesar added the plugin label May 12, 2015
@PAEz
Copy link

PAEz commented Nov 1, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants