I am writing a website socket server on nodejs, using redis, in particular of the same name lib. So, when I retrieve the answer, the script execution goes on without waiting for the callback call with the result. In a browser, I could use $ .Deferred. And what to do in nodejs?
Here is an example code for clarity:
console.log("STEP 1"); // ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° ΡΠ΅ΡΡΠΈΠΈ Π½Π° ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΠ΅ rc.exists("session:" + sid, function(err, res) { if (!res) { console.log((new Date()) + " Session not exists in db"); request.reject(401, "Authentication required"); } console.log("STEP 2"); }); console.log("STEP 3");
So the STEP 2 call comes last, but you need to do it in order.