Coder Social home page Coder Social logo

outside of client's scope? about node-redis HOT 3 CLOSED

NHQ avatar NHQ commented on May 26, 2024
outside of client's scope?

from node-redis.

Comments (3)

aeosynth avatar aeosynth commented on May 26, 2024

That's what you have to do with async functions in general, not just redis. There's a whole crop of modules (search for Flow Control / Async Goodies) dedicated to making async easier.

from node-redis.

mranney avatar mranney commented on May 26, 2024

Your second example should work though, but you are probably getting confused about the order in which things run. Check out this program:

var client = require("redis").createClient();

client.set("some key", "some val");
var data;
client.get("some key", function (err, res) {
    data = res;
    console.log("Inside callback, data is: " + data);
});
console.log("Outside callback, data is: " + data);

This will output:

Outside callback, data is: undefined
Inside callback, data is: some val

Note that the "Outside callback" ran first. Since client.get() is a potentially blocking operation, the callback is noted, and control passes immediately (as far as you are concerned) to the next line in your program.

Note also that client.set() and client.get() are both operations that complete asynchronously, but they are guaranteed to complete in order. So you know that the get will not invoke the callback until the set is complete.

from node-redis.

NHQ avatar NHQ commented on May 26, 2024

Ah-ha, thanks for that bit of tutorial.

from node-redis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.