How to set variable before callback is called?

Posted by user1995327 on Stack Overflow See other posts from Stack Overflow or by user1995327
Published on 2013-10-27T21:51:05Z Indexed on 2013/10/27 21:53 UTC
Read the original article Hit count: 210

Filed under:
|

I'm trying to design a webpage...

I have a function that I call get all info needed for an idividuals home page. A snippet of the codes is:

exports.getHomePageData = function(userId, cb) {
    var pageData = {};

    pageData.userFullName = dbUtil.findNameByUserId(userId, function(err){
        if (err) cb(err);
    });
    pageData.classes = dbUtil.findUserClassesByUserId(userId, function(err){
        if (err) cb(err);
    });

    cb(pageData);
}

The problem I'm having is the cb(pageData) is being called before I even finish setting the elements.

I've seen that people use the async library to solve this but I was wondering if there was any other way for me to do it without needing more modules...

Thanks!

© Stack Overflow or respective owner

Related posts about node.js

Related posts about asynchronous