Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript / NodeJs different async functions resulting in one big json file
    primarykey
    data
    text
    <p>I simply can't figure this out. I have a folder with files. I want to create a json with information of these files. To be precise the filename, a regexed version of the filename and additional data I get from a ajax call.</p> <p>The individual parts work perfectly. In fact, if I run my code in sync, I will get the result I want. Something like this:</p> <pre><code> [ { "filename" : "first file", "regexname" : "first file", "ajaxresult" : "first file" }, { "filename" : "second file", "regexname" : "second file", "ajaxresult" : "second file" }, ] </code></pre> <p>But I want to do it async because the XHR module I use doesn't work properly when trying to do everything 'in sync' and it's of course blocking especially with a lot of files. My 'in sync' code looks like this (a bit simplified to make it shorter )</p> <pre><code> fs.readdir(datapath, function(err, files) { var data = new Array(); if(err) throw err; files.forEach(function(file){ var filename = file , regex= filename.replace(/regex/)/i, "") , filepath = './public/config/dataconfig.js' , ajaxResults = getFile(url) , mapFiles = null mapFiles = { filename:filename, regexname:regex, ajaxcall:ajaxResults} data[data.length] = mapfiles; } var JSONdata= JSON.stringify(data, null, 4); fs.writeFile(filepath, JSONdata, function(e) { if (!e) { console.log('Done') }else{ console.log('Failed', e) }; }); }); </code></pre> <p>I tried wrapping the ajaxcall in a promise, but I lose the filename and regexname inside the promise. like so:</p> <pre><code> fs.readdir(datapath, function(err, files) { var data = new Array(); if(err) throw err; files.forEach(function(file){ var filename = file , regex= filename.replace(/regex/)/i, "") , filepath = './public/config/dataconfig.js' , mapFiles = null // Make promise for async call with a url composed of the regex var promise = getFile(url); promise.then( function(result) { mapFiles = { filename:filename, regexname:regex, ajaxcall:result} data[data.length] = mapfiles; var JSONdata= JSON.stringify(data, null, 4); fs.writeFile(filepath, JSONdata, function(e) { if (!e) { console.log('Done') }else{ console.log('Failed', e) }; }); }); }); </code></pre> <p>This gives me the following result:</p> <pre><code> [ { "filename" : "first file", "regexname" : "first file", "ajaxresult" : "first file" }, { "filename" : "first file", "regexname" : "first file", "ajaxresult" : "second file" }, ] </code></pre> <p>So my final thought was to use something like the async module for node. writing the json after I get callbacks back from the function. but I can't get that to work either.</p> <p>Thanks in advance!</p> <p><strong>[EDIT - what I forgot to mention ]</strong></p> <p>A thing I forgot to mention is that the AjaxCall url is built with the regex. So something like this: </p> <pre><code> url = 'http://www.test.com/'+regex </code></pre> <p>So this (as I see it) would mean I do the regex before calling the ajaxcall but I also need the regex after the call to write in the Json. I hope this makes sence</p> <p><strong>[EDIT 2 - A bit more information]</strong></p> <p>My XHR call looks like this (without my faulty promise solution. In that case I return the promise with the result):</p> <pre><code>// Get info function getFile(url) { xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.onreadystatechange = handler var ajaxResult = null //Callback function handler (){ // If status is ready if (this.readyState == 4 &amp;&amp; this.status == 200) { ajaxResult = eval(this.responseText); console.log('XHR OK') }; }; xhr.send(null); return ajaxResult[0]; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload