-3

I'm newbie on Node.js, because of its asynchronous sometimes I confused how can I control function execute order. Imagin there are two functions below.

function login(id, pw){
 // do something
}


function getMyInfo(){
 // do something
};

The login() function should work firstly. and then getMyInfo(). How can I do it? I tried to this :

login(id, pw, function(err){
  // do something 
  if(err) return next();

  getMyInfo();
});

But It doesn't work. How can I do it?

ton1
  • 7,238
  • 18
  • 71
  • 126

2 Answers2

0

This is called "callbacks"

function login(id, pw, cb){
 // do something

 // after you're done:
 cb();
}

function getMyInfo(){
 // do something
};
Louay Alakkad
  • 7,132
  • 2
  • 22
  • 45
0

Make use of callbacks.

Suppose you want that execution of function 2 must not take place before function 1 then call function 2 as successful callback for function 1.

They are like promises in other languages.

You can make use of Q module if you don't like callbacks.

http://www.tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm

Simple and Easy Tutorial

Gandalf the White
  • 2,415
  • 2
  • 18
  • 39