-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?

3
  • You are only declaring the functions, they are not executed. Commented Dec 27, 2015 at 22:23
  • You need to actually accept and call the callback. Commented Dec 27, 2015 at 22:25
  • @Pierre sorry I edited. Commented Dec 27, 2015 at 22:26

2 Answers 2

0

This is called "callbacks"

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

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

function getMyInfo(){
 // do something
};
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.