Not confident enough if this would be the correct 'node way' of writing Promises
:
Essentially, there are two async database calls, the result from both of these calls has to be collected and used in rendering the view
:
Creating ...
exports.getIndustryById = function(industryId) {
return new Promise(function(resolve, reject) {
Industry.findOne({
value: industryId
}, function(err, industry) {
if (err) {
return reject(err);
}
return resolve(industry);
});
})
}
exports.getCountryById = function(countryId) {
return new Promise(function(resolve, reject) {
Country.findOne({
value: countryId
}, function(err, country) {
if (err) {
return reject(err);
}
return resolve(country);
});
})
}
And using ...
exports.renderEditProfile = function(req, res, next) {
var i, c;
ct.getIndustryById(req.user.industry)
.then(function(industry) {
i = industry;
return ct.getCountryById(req.user.country);
}).then(function(country) {
c = country;
res.render('profile_edit', {
profile: req.user,
industry: i,
country: c
});
})
.catch(function(err) {
res.status(500).json({'message': err});
});
};
This works, but am I getting Promises right?