Wait for all asynchronous calls before returning in ES5 pure JS? [duplicate]
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;
);
javascript asynchronous
marked as duplicate by Quentin
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.
add a comment |
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;
);
javascript asynchronous
marked as duplicate by Quentin
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 onthen,..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
add a comment |
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;
);
javascript asynchronous
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
javascript asynchronous
asked Nov 14 '18 at 13:12
RedHorseRedHorse
304
304
marked as duplicate by Quentin
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
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 onthen,..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
add a comment |
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 onthen,..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
add a comment |
1 Answer
1
active
oldest
votes
let promises =
features.forEach(function(feature)
promises.push(locator.locationToAddress(feature.geometry))
);
Promise.all(promises).then(x =>
/* ... */
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
let promises =
features.forEach(function(feature)
promises.push(locator.locationToAddress(feature.geometry))
);
Promise.all(promises).then(x =>
/* ... */
add a comment |
let promises =
features.forEach(function(feature)
promises.push(locator.locationToAddress(feature.geometry))
);
Promise.all(promises).then(x =>
/* ... */
add a comment |
let promises =
features.forEach(function(feature)
promises.push(locator.locationToAddress(feature.geometry))
);
Promise.all(promises).then(x =>
/* ... */
let promises =
features.forEach(function(feature)
promises.push(locator.locationToAddress(feature.geometry))
);
Promise.all(promises).then(x =>
/* ... */
answered Nov 14 '18 at 13:18
enno.voidenno.void
1,82121026
1,82121026
add a comment |
add a comment |
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