I am using closure inside for loop to save categories to categories
table after article
is saved.
article.save(function (err, newArticle) {
if (err) throw err;
console.log('article created ', newArticle._id);
for (var i = 0; i < categories.length; i++) {
(function (index) {
var category_article = new category_article_model({
"category": categories[index],
"article_id": newArticle._id
});
category_article.save(function (err, new_category_article) {
if (err) throw err;
})
}(i));
}
return res.status(res.statusCode).send(newArticle);
})
How do I convert the above to use promises?