I pass the data to the function for processing in order to later write the result of the processing into a text file. Judging by the sequence of logging, I understand that the problem is that the value does not have time to return, but I don’t know how to deal with it. I really need advice. Actually the function itself:

function convertMe(polygonArray){ var toWrite = ''; var dataToReturn = ''; var polygon = JSON.parse(polygonArray); var isFirst = true; var printFeature = function (name, poly) { var out = { "type": "Feature", "properties": { 'name': name, }, "geometry": { "type": "Polygon", "coordinates": poly } }; out = JSON.stringify(out); if(!isFirst) { out = ',' + out } isFirst = false; toWrite = toWrite + out; } toWrite = toWrite + '{"type": "FeatureCollection","features": ['; async.mapSeries([polygon], function (poly, cb) { var options = { coords: poly, precision: 6, rowMode: true, hashMode: 'extent', threshold: 0.2 }; printFeature('shape', poly); var rowStream = hasher.stream(options), a = 0; rowStream .on('end', cb) .pipe(through2.obj(function (arr, enc, callback) { for(var i = 0; i < arr.length; i++) { var bb = ngeohash.decode_bbox(arr[i]); printFeature(arr[i], [ [ [bb[1], bb[2]], [bb[3], bb[2]], [bb[3], bb[0]], [bb[1], bb[0]], [bb[1], bb[2]] ]]); } callback(); })); }, function () { toWrite = toWrite + ']}'; console.log('Внутри вызова '+toWrite); return toWrite; }); }; 

Place her call:

 worldData.features.forEach(function(feature){ q.defer(function(done){ var dataToConvert = JSON.stringify(feature.geometry.coordinates); console.log('Место вызова '+convertMe(dataToConvert)); //fs.writeFileSync(currDir+'/'+outputDir+'/'+'cell_'+i+'.s2', convertMe(dataToConvert)); i++; done() }); }); 

Output to console:

 Место вызова undefined Внутри вызова {"type": "FeatureCollection","features": [{"type":"Feature","properties":{"name":"shape"},"geometry":{"type":"Polygon","coordinates":[[[[-56.0828202,46.8785071],[-56.0832065,46.8780553],[-56.0840219,46.877938],[-56.0843308,46.8776271],[-56.0848973,46.8777385],[-56.0852407,46.8776564],[-56.0858329,46.8777385],[-56.0865109,46.8773278],[-56.0874293,46.8769875],[-56.0878327,46.877146],[-56.0880559,46.8770697],[-56.0886396,46.8767118],[-56.0895236,46.8764771],[-56.0898154,46.8763011],[-56.0900643,46.8759197],[-56.0904506,46.8756733],[-56.0915664,46.8757554],[-56.0923045,46.8757437],[-56.0929568,46.8755618],[-56.0933002,46.8758024],[-56.0939696,46.8763656],[-56.0944245,46.8769054],[-56.0951026,46.8773689],[-56.0945619,46.8778089],[-56.0939868,46.8784015],[-56.0929483,46.8789295],[-56.0917295,46.8794399],[-56.0907767,46.8796981],[-56.0896781,46.8799797],[-56.0895408,46.8801029],[-56.0891803,46.8802613],[-56.0892404,46.8804549],[-56.0888112,46.88076],[-56.0883048,46.880801],[-56.0880044,46.8810533],[-56.0866397,46.8813232],[-56.0853265,46.8811354],[-56.0846227,46.8810357],[-56.0850347,46.8808069],[-56.084163,46.8808322],[-56.083258,46.8804314],[-56.0830348,46.8801791],[-56.0830434,46.8797333],[-56.0830863,46.8794399],[-56.0837043,46.8792757],[-56.0834468,46.8791994],[-56.0835841,46.8790586],[-56.0833266,46.8789706],[-56.0832665,46.8788239],[-56.0835498,46.8784308],[-56.0830348,46.8786948],[-56.0828202,46.8785071]]]]}},{"type":"Feature","properties":{"name":"fb24ew"},"geometry":{"type":"Polygon","coordinates":[[[-56.09619140625,46.8841552734375],[-56.085205078125,46.8841552734375],[-56.085205078125,46.878662109375],[-56.09619140625,46.878662109375],[-56.09619140625,46.8841552734375]]]}},{"type":"Feature","properties":{"name":"fb24ey"},"geometry":{"type":"Polygon","coordinates":[[[-56.085205078125,46.8841552734375],[-56.07421875,46.8841552734375],[-56.07421875,46.878662109375],[-56.085205078125,46.878662109375],[-56.085205078125,46.8841552734375]]]}},{"type":"Feature","properties":{"name":"fb24et"},"geometry":{"type":"Polygon","coordinates":[[[-56.09619140625,46.878662109375],[-56.085205078125,46.878662109375],[-56.085205078125,46.8731689453125],[-56.09619140625,46.8731689453125],[-56.09619140625,46.878662109375]]]}},{"type":"Feature","properties":{"name":"fb24ev"},"geometry":{"type":"Polygon","coordinates":[[[-56.085205078125,46.878662109375],[-56.07421875,46.878662109375],[-56.07421875,46.8731689453125],[-56.085205078125,46.8731689453125],[-56.085205078125,46.878662109375]]]}}]} 

That is, first I get Undefined from the call site, and only then the result of the work (by the way, the right one and the one I need) in place

Reported as a duplicate by Grundy , pavel , user207618, aleksandr barakin , Vartlok Aug 30 '16 at 12:59 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Well, what do you want from async ? It should be so. Do not use asynchronous operations and there will be no such problems - ArchDemon
  • @Grundy thanks for the link, but at least I can't understand why then in the log inside the call it displays the value normally? return is after this log, there is data in the log, there is no return. - justyx
  • First of all, your convertMe function convertMe not convertMe value at all. The only return in the callback function of async.mapSeries , namely return toWrite; . Secondly, even if you write return for the convertMe function, it won't work for you anyway. - Stepan Kasyanenko
  • one
    It is necessary not to use debug consollogs, but using breakpoints. Set breakpoints and execute your code step by step. look at how all variables change (whether they change as you think), look at what place the interpreter in the console gives an error - Dimon
  • @justyx, but it seemed to me that the link explains why return in the callback of an asynchronous function does not work as you expect - Grundy

1 answer 1

It is necessary to pass a function (callback) to the convertMe function, which must be called when the async.mapSeries asynchronous function async.mapSeries .

Example:

 function convertMe(polygonArray,callbackConvert){ var toWrite = ''; var dataToReturn = ''; var polygon = JSON.parse(polygonArray); var isFirst = true; var printFeature = function (name, poly) { var out = { "type": "Feature", "properties": { 'name': name, }, "geometry": { "type": "Polygon", "coordinates": poly } }; out = JSON.stringify(out); if(!isFirst) { out = ',' + out } isFirst = false; toWrite = toWrite + out; } toWrite = toWrite + '{"type": "FeatureCollection","features": ['; async.mapSeries([polygon], function (poly, cb) { var options = { coords: poly, precision: 6, rowMode: true, hashMode: 'extent', threshold: 0.2 }; printFeature('shape', poly); var rowStream = hasher.stream(options), a = 0; rowStream .on('end', cb) .pipe(through2.obj(function (arr, enc, callback) { for(var i = 0; i < arr.length; i++) { var bb = ngeohash.decode_bbox(arr[i]); printFeature(arr[i], [ [ [bb[1], bb[2]], [bb[3], bb[2]], [bb[3], bb[0]], [bb[1], bb[0]], [bb[1], bb[2]] ]]); } callback(); })); }, function () { toWrite = toWrite + ']}'; callbackConvert(toWrite);// Вызов функции callback при завершении работы. console.log('Внутри вызова '+toWrite); }); }; 

Call convertMe and transfer callback .

 worldData.features.forEach(function(feature) { q.defer(function(done) { var dataToConvert = JSON.stringify(feature.geometry.coordinates); //Запускаем функцию convertMe и передаем функцию обработчик завершения выполнения convertMe(dataToConvert, function(result) { console.log('Место вызова ' + result); i++; done(); //вызвать done, только когда convertMe завершит работу }); }); }); 

  • I have already managed to figure out, fortunately, but thanks anyway for an example of how to do this in my case, the kolbeks in my head are very tight. Although judging by the fact that I could do all the clarification then there is :) - justyx