how to get the "get" command real value isn't a 'true'?
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.
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.
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
Thanks, @brycebaril I am clear now , the issue that the executing of callback function is asynchronous.
Most helpful comment
Hi @yuany -- this is a traditional Node.js library in that you provide callbacks to execute asynchronously when the results are ready.
I.e.:
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.