Wait for all asynchronous calls before returning in ES5 pure JS? [duplicate]










0
















This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



There are two asynchronous calls (@ .then(...)) in this function which get looped through multiple times. How can I restructure this function to ensure it only gets returns when all calls have been completed? I know I could use async/await in ES6 but I can only use ES5 with no libraries.



function queryLayer(layer, buffer)
var query = layer.createQuery();
query.geometry = buffer;
query.spatialRelationship = "intersects";

layer.queryFeatures(query).then(function(results)
console.log(results.features[0].sourceLayer.title, results.features.length);
if(!results.features.length > 0) return false;
var features = results.features;
features.forEach(function(feature)
feature.distance = calculateDistance(buffer.centroid, feature.geometry);
locator.locationToAddress(feature.geometry).then(function(x)
feature.address = x.address;
);
);
return features;
);










share|improve this question













marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • You probably have to use callbacks

    – Sebastian Speitel
    Nov 14 '18 at 13:13











  • Pretty impossible to return with asynchronous methods

    – epascarello
    Nov 14 '18 at 13:14











  • Return the promise, and then the calling end can act on then,.. return layer.queryFeatures(query)

    – Keith
    Nov 14 '18 at 13:15






  • 1





    Return the promise of each call and then use Promise.all()

    – pinoyyid
    Nov 14 '18 at 13:15















0
















This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



There are two asynchronous calls (@ .then(...)) in this function which get looped through multiple times. How can I restructure this function to ensure it only gets returns when all calls have been completed? I know I could use async/await in ES6 but I can only use ES5 with no libraries.



function queryLayer(layer, buffer)
var query = layer.createQuery();
query.geometry = buffer;
query.spatialRelationship = "intersects";

layer.queryFeatures(query).then(function(results)
console.log(results.features[0].sourceLayer.title, results.features.length);
if(!results.features.length > 0) return false;
var features = results.features;
features.forEach(function(feature)
feature.distance = calculateDistance(buffer.centroid, feature.geometry);
locator.locationToAddress(feature.geometry).then(function(x)
feature.address = x.address;
);
);
return features;
);










share|improve this question













marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • You probably have to use callbacks

    – Sebastian Speitel
    Nov 14 '18 at 13:13











  • Pretty impossible to return with asynchronous methods

    – epascarello
    Nov 14 '18 at 13:14











  • Return the promise, and then the calling end can act on then,.. return layer.queryFeatures(query)

    – Keith
    Nov 14 '18 at 13:15






  • 1





    Return the promise of each call and then use Promise.all()

    – pinoyyid
    Nov 14 '18 at 13:15













0












0








0









This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



There are two asynchronous calls (@ .then(...)) in this function which get looped through multiple times. How can I restructure this function to ensure it only gets returns when all calls have been completed? I know I could use async/await in ES6 but I can only use ES5 with no libraries.



function queryLayer(layer, buffer)
var query = layer.createQuery();
query.geometry = buffer;
query.spatialRelationship = "intersects";

layer.queryFeatures(query).then(function(results)
console.log(results.features[0].sourceLayer.title, results.features.length);
if(!results.features.length > 0) return false;
var features = results.features;
features.forEach(function(feature)
feature.distance = calculateDistance(buffer.centroid, feature.geometry);
locator.locationToAddress(feature.geometry).then(function(x)
feature.address = x.address;
);
);
return features;
);










share|improve this question















This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



There are two asynchronous calls (@ .then(...)) in this function which get looped through multiple times. How can I restructure this function to ensure it only gets returns when all calls have been completed? I know I could use async/await in ES6 but I can only use ES5 with no libraries.



function queryLayer(layer, buffer)
var query = layer.createQuery();
query.geometry = buffer;
query.spatialRelationship = "intersects";

layer.queryFeatures(query).then(function(results)
console.log(results.features[0].sourceLayer.title, results.features.length);
if(!results.features.length > 0) return false;
var features = results.features;
features.forEach(function(feature)
feature.distance = calculateDistance(buffer.centroid, feature.geometry);
locator.locationToAddress(feature.geometry).then(function(x)
feature.address = x.address;
);
);
return features;
);





This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers







javascript asynchronous






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:12









RedHorseRedHorse

304




304




marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You probably have to use callbacks

    – Sebastian Speitel
    Nov 14 '18 at 13:13











  • Pretty impossible to return with asynchronous methods

    – epascarello
    Nov 14 '18 at 13:14











  • Return the promise, and then the calling end can act on then,.. return layer.queryFeatures(query)

    – Keith
    Nov 14 '18 at 13:15






  • 1





    Return the promise of each call and then use Promise.all()

    – pinoyyid
    Nov 14 '18 at 13:15

















  • You probably have to use callbacks

    – Sebastian Speitel
    Nov 14 '18 at 13:13











  • Pretty impossible to return with asynchronous methods

    – epascarello
    Nov 14 '18 at 13:14











  • Return the promise, and then the calling end can act on then,.. return layer.queryFeatures(query)

    – Keith
    Nov 14 '18 at 13:15






  • 1





    Return the promise of each call and then use Promise.all()

    – pinoyyid
    Nov 14 '18 at 13:15
















You probably have to use callbacks

– Sebastian Speitel
Nov 14 '18 at 13:13





You probably have to use callbacks

– Sebastian Speitel
Nov 14 '18 at 13:13













Pretty impossible to return with asynchronous methods

– epascarello
Nov 14 '18 at 13:14





Pretty impossible to return with asynchronous methods

– epascarello
Nov 14 '18 at 13:14













Return the promise, and then the calling end can act on then,.. return layer.queryFeatures(query)

– Keith
Nov 14 '18 at 13:15





Return the promise, and then the calling end can act on then,.. return layer.queryFeatures(query)

– Keith
Nov 14 '18 at 13:15




1




1





Return the promise of each call and then use Promise.all()

– pinoyyid
Nov 14 '18 at 13:15





Return the promise of each call and then use Promise.all()

– pinoyyid
Nov 14 '18 at 13:15












1 Answer
1






active

oldest

votes


















0














let promises = 
features.forEach(function(feature)
promises.push(locator.locationToAddress(feature.geometry))
);

Promise.all(promises).then(x =>
/* ... */






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    let promises = 
    features.forEach(function(feature)
    promises.push(locator.locationToAddress(feature.geometry))
    );

    Promise.all(promises).then(x =>
    /* ... */






    share|improve this answer



























      0














      let promises = 
      features.forEach(function(feature)
      promises.push(locator.locationToAddress(feature.geometry))
      );

      Promise.all(promises).then(x =>
      /* ... */






      share|improve this answer

























        0












        0








        0







        let promises = 
        features.forEach(function(feature)
        promises.push(locator.locationToAddress(feature.geometry))
        );

        Promise.all(promises).then(x =>
        /* ... */






        share|improve this answer













        let promises = 
        features.forEach(function(feature)
        promises.push(locator.locationToAddress(feature.geometry))
        );

        Promise.all(promises).then(x =>
        /* ... */







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 13:18









        enno.voidenno.void

        1,82121026




        1,82121026















            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus