How to call external function inside callback in nodejs
<p>Calling a external functions in nodejs as we do in javascript so that we can re-use the function but in my case its not working. Why?</p>

<p>I am guessing its working asynchronously in nodejs. How do I fix this? Callbacks? I am new to nodejs.</p>

<pre><code>app.get('/googleSyncCallback', passport.authenticate('google', {
  failureRedirect: 'url'
}),

function(req, res) {

  var contacts = new GoogleContacts({
    token: req.token
  });
  var retrievedContacts = '';
  var retrievedContactsArr = [];

  contacts.getContacts(function(err, contacts) {
    contacts.forEach(function(entry) {
      retrievedContacts = {
        contact: {
          "phone": {
            "cell": entry.phoneNumber,
          }
        },
      }
      retrievedContactsArr.push(retrievedContacts);
      console.log('array... ', retrievedContactsArr[0]); //prints all values
    });
  });
  checkIfContactExists(req, res, retrievedContactsArr);
}
});

function checkIfContactExists(req, res, retrievedContactsArr) {
  //PRINTS UNDEFINED..
  console.log('array... ', retrievedContactsArr[0]); //// LINE 10
}
</code></pre>

<p>printing the array <code>retrievedContactsArr[0]</code> is returning  undefined at line 10.</p>