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

how to get the "get" command real value isn't a 'true' #475

Closed
yuany opened this issue Aug 12, 2013 · 4 comments
Closed

how to get the "get" command real value isn't a 'true' #475

yuany opened this issue Aug 12, 2013 · 4 comments
Labels

Comments

@yuany
Copy link

yuany commented Aug 12, 2013

how to get the "get" command real value isn't a 'true'?

@brycebaril
Copy link
Contributor

Hi @yuany -- this is a traditional Node.js library in that you provide callbacks to execute asynchronously when the results are ready.

I.e.:

client.get("foo", function (error, value) { /* ... */ })

This is a result of the way that Node.js works, IO operations are done asynchronously.

The return value for these functions is not the value.

@yuany
Copy link
Author

yuany commented Aug 13, 2013

Thanks, @brycebaril I have tried the function like this:

var result = client.get("foo", function (error, value) { return value});
alert(result);//the result will be 'false' or 'true', but I want to return value is real value by the key from redis.

@brycebaril
Copy link
Contributor

Hi @yuany -- That's not how node works when it is doing IO.

The callback provides a context to execute when the result is available. All other code that doesn't depend on the IO will run while the IO operation is queued, executed, and the reply is waited for.

Here's a good place to learn more about how it works: https://github.com/maxogden/art-of-node#callbacks

E.g.

client.get("foo", function (error, value) {
  // value is only defined in the context of this callback
  console.log(value)
})

// this parent code has already executed, before it asked Redis for the value

@yuany
Copy link
Author

yuany commented Aug 13, 2013

Thanks, @brycebaril I am clear now , the issue that the executing of callback function is asynchronous.

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

3 participants