O
O
OVK20152016-02-25 18:44:46
Node.js
OVK2015, 2016-02-25 18:44:46

Request and async in Node.JS What did I miss?

I'm trying to get the sizes of video clips from youtube:

var async = require('async')
var request = require('request');

var moveInfoLink = "http://www.youtube.com/get_video_info?video_id=",
  moveLinkSufix = "&asv=3&el=detailpage&hl=en_US",
  movieWrapperLink = "*********";

var answer = '';
var formatList = new Array();
var movieLinks = new Array();

function getYouTubeAnswer()
{
  return new Promise(function(resolve)
  {
    ........
  });
}

function parseYouTubeData()
{	
...............
}

var getFileSize = function(item, callback)
{	
  if(item['resolution'].match(/x720|x360/))
  {
    request
    (	
      {
        method: 'HEAD',
        url: item['url']
      },
      function(err, resp, body)
      {				
        console.log(resp.headers['content-length']);
        callback(false, resp.headers['content-length']);
      }
    );				
  }	
}

console.log(`Запускаемся`);
getYouTubeAnswer().then(function()
{	
  parseYouTubeData();	
  console.log(`Получаем размеры файлов`);
  async.each
  (
    movieLinks,		
    getFileSize,
    function(err, result)  
    {			// Вот сюда управление почему-то не попадает
      if(err)			
      {
        console.log('Какая-то ошибка');		
        console.log(err);		
      }
      else
      {
        console.log('Закончили работу');	
        console.log(result);				
      }			
    }
  );	
});

The idea is the following:
1. I get data about a specific clip.
2. I get all its options available for download.
3. I get the file sizes for the variants of interest.
All data of interest is received correctly. But, I can’t understand why the code in the last function inside async is not executed. That is, control is not transferred to this function at all.
What did I miss?
As a result, I would like to wait for the execution of all requests that receive the sizes of individual clip options, and then generate a general report.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav Yandimirkin, 2016-02-25
@OVK2015

Did you read the async documentation correctly?
each(arr, iteratee, [callback])
getFileSize is an iterator? does it return a callback on execution?

A
Alexander, 2016-02-26
@w4r_dr1v3r

The idea is the following:
1. I get data about a specific clip.
2. I get all its options available for download.
3. I get the file sizes for the variants of interest.

Um... is this SaveFrom #2 or am I missing something?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question