I want to push an object into array that one of the field is value need to get from async function
function loadContentAsync(url) {
var deferred = Q.defer();
request(url, function (err, res, html) {
if (!err) {
deffered.resolve(html);
}
});
return deffered.promise;
}
var aList = $('#mCSB_5_container ul').find('a');
console.log(c);
var list = [];
aList.each(function (i, elem) {
var html = loadContentAsync(this.attribs.href);
list.push({
title: $(this).text(),
url: this.attribs.href,
content: html
});
});
But when I run this code, because loadContentAsync
function doesn't return value synchronously, the html field will be null.
How can I this field asynchronously?