If a parent doesn't take a callback, but its children do, will the parent be ran asynchronously?

45 views
Skip to first unread message

sam

unread,
Jul 13, 2015, 1:59:49 AM7/13/15
to nod...@googlegroups.com

```
/* somewhere deep inside a module... */
var token = Tokens.create();
pair(email, token);
send(email, token); 
```

```
/* in another module */
Tokens.create = function() {
  crypto.randomBytes(48, function(ex, buf) {
    var token = buf.toString('hex');
    return token;
  })
}
```

Will the `Tokens.create()` be ran in parallel to `pair(email, token)` and `send(token, email)`? I'm trying to understand if i need to use a callback or not here

Someone in IRC responded:
"just google for callbacks, in a nutshell when the second argument of function is a function"

Tokens.create doesn't take a callback, but the function it calls, `crypto.randomBytes`, does. So will `token = Tokens.create` be ran asynchronously?

Bruno Jouhier

unread,
Jul 13, 2015, 9:45:09 AM7/13/15
to nod...@googlegroups.com
As you wrote it Tokens.create will not return your token; it will return undefined! The return token statement is inside the callback, not inside Tokens.create.

To make this work you have to turn Tokens.create into an async function and call it with a callback:


    Tokens.create(function(ex, token) {
      if (ex) throw ex;
      pair(email, token);
      send(email, token);
    });
    
    Tokens.create = function(cb) {
      crypto.randomBytes(48, function(ex, buf) {
        if (ex) return cb(ex);
        var token = buf.toString('hex');
        cb(null, token);
      })
    }

Async is contagious: if a function needs a value which is returned asynchronously by another function, it must be asynchronous.

Bruno

Shailendra Sen

unread,
Jul 13, 2015, 9:45:50 AM7/13/15
to nod...@googlegroups.com
function encrypt(text){
  var algorithm = 'AES-256-CBC';
  var password = 'd6F3Efeq';
  var cipher = crypto.createCipher(algorithm,password)
  var crypted = cipher.update(text,'utf8','hex')
  crypted += cipher.final('hex');
  return crypted;
}

function dcrypt(encryptGenPwd)
{
  var algorithm = 'AES-256-CBC';
  var password = 'd6F3Efeq';
  var decipher = crypto.createDecipher(algorithm,password)
  var dec = decipher.update(encryptGenPwd,'hex','utf8')
  dec += decipher.final('utf8');
  return dec;
}

exports.forgotpassword = function(req,res){
  var newUser = new User(req.body);
  var forgotemail = req.body.forgotemail;
    var err="";
  async.waterfall([
    function (done){

      var token = encrypt(forgotemail);
     
      if (token=="" || !token)
      {
         crypto.randomBytes(20,function(err,buf){

        var token = buf.toString('hex');
       done(err,token);
      });   
      }else{
      done(err,token);
      }
     },  
   function (token,done){
      User.findOne({email:forgotemail},function(err,user){

        if (!user){
          res.json({success:false,message:'Email ID not Exists'});
        }
        else{ 
      User.update({email:forgotemail},{$set: {resetPasswordToken: token,resetPasswordExpires:Date.now()}},function(err,user){
          if(err){
            res.json({success:true,message:'Error While updating Data'});
          }
          else if(!user){
           res.json({success:true,message:'Oops something went wrong please try again later'});
          }
          else{
            done(err, token, user);
          }
        }); 
        }
      });
    },
    
  function(token,user, done){
    var smtpTransport = nodemailer.createTransport("SMTP",{
                        service: "Gmail",
                        auth: {
                            user: "@gmail.com",
                            pass: "  "
                        }
                    }); 
    var mailOptions = {

                    from: " <shailendr...@gmail.com>",
                        to: "Receiver Name <" + req.body.forgotemail + ">",
                           subject: "Forgot your password",
                             text: "",
                                html: "<p>we've received  your request to reset the password for this email address.</p>"
                                  + " To reset your password please click on this link or cut and paste this URL into your browser.<br>" +
                                    'http://' + req.headers.host + '/resetpassword?token=' + token + '\n\n'
                                      }
   
        smtpTransport.sendMail(mailOptions,function(err){
        res.json({success : true, message : "An Email Has been sent to th provided Email Address"});
        done(err,'done');
      });
    }
    ]);

}


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/dfcad4b1-b068-4743-8915-1c70f1b54efa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best Regards
Shailendra Sen
LinkITes Infotech Pvt Ltd
Reply all
Reply to author
Forward
0 new messages