There is such a function:

var p; function readFile(fileEntry) { fileEntry.file(function (file) { p = 'Name: ' + file.name + 'Last Modified: ' + new Date(file.lastModifiedDate) + 'Size: ' + file.size; }); } 

then I call its variable p in another function:

 function listResults(entries) { entries.forEach(function (entry) { if (entry.isDirectory) { //.... } else { readFile(entry); console.log(p); } }); } 

The variable does not give what I have assigned, but writes undefined, what can it be? How can I solve this problem with my specific example? Long ago I have been tormenting this question, no one can help. Help me please. I use File API for JS.

The first function receives data on files, the second should display this data.

    2 answers 2

    If the statement from the past question that fileEntry.file () is asynchronous is true, then the task changes.

    Since it is unlikely to arrange the further processing in the callback function (as far as I understand you want to send information on all files in one request), the second option remains:

    Write a separate function that will check whether all the answers to the sent requests came and only after that will it process the received data. Add a counter that will count the number of sent and received data (when sending a request, we increase the counter by 1, when receiving the data we decrease). If the counter is non-zero, then not all the data has yet come.

    An example in a hurry. You can already work in this direction to achieve the desired result.

     var p=[], k=0; //объявляем счетчик и массив для данных function readFile(fileEntry){ fileEntry.file(function(file){ p.push('Name: '+file.name+ //пушим данные в массив для дальнейшей обработки 'Last Modified: '+new Date(file.lastModifiedDate)+ 'Size: '+file.size); k--; //понижаем счетчик (данные записались) sendResults(); // вызываем функцию для обработки данных }); } function listResults(entries) { entries.forEach(function(entry) { if (entry.isDirectory) { //.... } else { k++; //увеличиваем счетчик (данные запросили) readFile(entry); } }); } function sendResults(){ //функция для отправки данных if(k==0){ //если счетчик 0, значит полученных ответов столько же сколько отправленных запросов. Можно готовить данные к отправке console.log(p.join('|')); } } 

    PS I apologize too fast. I still have to check the total number of files I think. As if the first readFile (entry); will work very quickly (before the next one goes to forEach ()), then the sending function will work with the data part.

    • one
      added an example - iKuzko
    • @dajver - once again "subtly hinting." Learn the materiel, read a couple of books, write a little academic code. Or will you be old enough to write on hashcode on any issue related to javascript? :) - Zowie
    • I have already figured out, thanks @iKuzko, for that! - dajver
     function readFile(fileEntry){ var p; fileEntry.file(function(file){ ... }); return p; } 

    and then, instead

     readFile(entry); console.log(p); 

    simply

     console.log(readFile(entry)); 

    as it turned out that fileEntry.file () is asynchronous

     function readFile(fileEntry, callback){ fileEntry.file(function(file){ callback('Name: '+file.name+...); }); } 

    and further:

     readFile(entry, console.log); 
    • one
      This is a past stage. So we tried before it turned out that fileEntry.file () is asynchronous :) - iKuzko
    • Returns undefined - dajver