Node-redis: how to get the "get" command real value isn't a 'true'

Created on 12 Aug 2013  路  4Comments  路  Source: NodeRedis/node-redis

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

fixed / done question

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.:

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dotSlashLu picture dotSlashLu  路  5Comments

gpascale picture gpascale  路  4Comments

abhaygarg picture abhaygarg  路  5Comments

id0Sch picture id0Sch  路  4Comments

twappworld picture twappworld  路  7Comments