I am using async module on node.js from yesterday to handle the order of asynchronous tasks. Below code works, But the error:
Callback was already called
shown about 1 to 5 times. I don't know where is the problem, maybe the callback in forEach loop on getNewsTitles() called not just one time. So I put console.log in here but this log printed only one time whether error is shown or not.
async.waterfall([ function(callback) { callback(); }, function(callback) { // When error, below log doesn't show. console.log('getting news titles...'); getNewsTitles(arr_uri, arr_subs, function() { // * The problem is here => "Callback was already called" callback(null); }); } ], function(err, result) { if (err) return next(); else res.send(arr_subs); }); function getNewsTitles(targets, subs, callback) { targets.forEach(function(current, index) { request.get({ uri: current, encoding: null }, function(err, response, body) { if (!err && response.statusCode == 200) { var $ = cheerio.load(iconv.decode(body, 'EUC-KR')); var subject = $('.articleSubject a'); for (var i = 0; i < subject.length; i++) { subs.push(subject[i].attribs.title); } if (subs.length == (targets.length - 2) * 20 + 2) { // when error or not, below log shows one time. console.log('doubt here too'); callback(); } } }); }) } Did I miss something..?