0

I am not sure what the callback function should look like if I want to do this. Both functions are in a class and I really want to abstract it.

doesexist: function(setvalue) {
  redisclient.sismember('setname', setvaue, callbackfuntion(value));
}

someothermethod: function() {
  if (doesexist()){
    // doSomething
  }
}

How would I do that in an async environment?

UPDATE:

I now tried it that way (coffeescript):

deoesexist: (setvalue, cb) ->
  @r.sismember 'setname', setvalue, (err, res) -> cb(res)

someothermethod: (setvalue) ->
  @doesexist setvalue, (exists) =>
    unless exists
      # emit an event that calls a function
      # that probably adds the not existing value.
      # I just don't want to redo this. That's
      # what this function is all about

It seems to work pretty well like that.

user1680104
  • 8,437
  • 4
  • 22
  • 27
  • Take a look at my [answer from earlier](http://stackoverflow.com/questions/14057140/sqllite-access-in-javascript/14060759#14060759) dealing with a similar manner of querying a WebSQL database (in Chrome). Note in particular the recursive `populate` calls when I'm `DROP`ing and `CREATE`ing before I loop the `INSERT`s. If you want to run that script as well, the only way I could get past the security error in jsFiddle was to run it locally as well, as in save it to your desktop. – Jared Farrish Dec 28 '12 at 10:33
  • 1
    So did you figure it out? I was going to post an answer about what you're actually after here, which is a *deferred promise* for your query. See [this question](http://stackoverflow.com/questions/6801283/what-are-the-differences-between-deferred-promise-and-future-in-javascript), this [node.js focused explanation](http://howtonode.org/promises), [node.js Q](https://github.com/kriskowal/q) (look [here](https://github.com/kriskowal/q/wiki/Examples-Gallery) for a CoffeeScript example link), and [the node/promise motherload](https://github.com/kriskowal/q/wiki/Examples-Gallery). Nifty stuff. – Jared Farrish Dec 28 '12 at 11:32

2 Answers2

0

You should checkout out control flow libraries, https://npmjs.org/browse/keyword/flow, to determine which approach is best for you, I prefer the async library https://github.com/caolan/async

To answer your question you would work your problem in the following way;

doesexist: function(setvalue, callback) {
    redisclient.sismember('setname', setvaue, callback);
}

someothermethod: function(somevalue) {
   doesexist(somevalue, function(err, exists) {
         if (exists) {
            // doSomething
         }
   })
}

I hope that helps!

Jay

Jason Brumwell
  • 3,482
  • 24
  • 16
  • Yes, I looked at the libraries, but do they really make that example easier? I like async's features, but I think they are kinda unnecessary for what I am doing here or I just can't figure out the right function. I had a look at async. What feature would you use for my problem? – user1680104 Dec 29 '12 at 09:14
  • You are correct async and other flow control libraries are really useful when you have more then one nested callback. In this case its easier and more efficient to do it the way provided above. I mentioned them for reference once you get into more complex situations. – Jason Brumwell Dec 30 '12 at 13:42
0

I figured it out. One basically replaces the if statement with the callback function. Simple, if you think more high level about what you are actually doing, but hard when you are just not used to it.

See my update for the answer.

user1680104
  • 8,437
  • 4
  • 22
  • 27