According to this article: /@bluepnume/learn-about-promises-before-you-start-using-async-await-eb148164a9c8
It seems like it could be possible to use below syntax:
let [foo, bar] = await Promise.all([getFoo(), getBar()]);
for multiple promises execution. However while using it I get Uncaught SyntaxError: Unexpected identifier
.
How can i use async/await
and promise.all
to achieve multiple simultaneous operations executed and one resolve with a response.
-----EDITED
the function i am using inside promise.all
is this one:
async function getJson(callback) {
try {
let response = await fetch('URL_LINK_HERE');
let json = await response.json();
return json;
} catch(e) {
console.log('Error!', e);
}
}
as a test field i am using google chrome Version 60.0.3112.113
According to this article: https://medium./@bluepnume/learn-about-promises-before-you-start-using-async-await-eb148164a9c8
It seems like it could be possible to use below syntax:
let [foo, bar] = await Promise.all([getFoo(), getBar()]);
for multiple promises execution. However while using it I get Uncaught SyntaxError: Unexpected identifier
.
How can i use async/await
and promise.all
to achieve multiple simultaneous operations executed and one resolve with a response.
-----EDITED
the function i am using inside promise.all
is this one:
async function getJson(callback) {
try {
let response = await fetch('URL_LINK_HERE');
let json = await response.json();
return json;
} catch(e) {
console.log('Error!', e);
}
}
as a test field i am using google chrome Version 60.0.3112.113
-
4
What JS engine are you using? Is it one that supports
await
? – Quentin Commented Sep 11, 2017 at 15:40 -
5
Is the
await
within anasync
function? – Steven Goodman Commented Sep 11, 2017 at 15:43 -
2
Could you post the code that utilizes
Promise.all
itself? IfgetJson
is working well, it's most likely something with how you're callingPromise.all
or the context you're calling it from. – Steven Goodman Commented Sep 11, 2017 at 15:51 -
2
You need to return
response
fromgetJson
, right? Although I don't see how that would cause the syntax error your report. By the way, you might want to think twice about thecatch
ingetJson
, it will prevent the promise rejection from falling through. – user663031 Commented Sep 11, 2017 at 15:58 -
3
Personally, I'd let the error in
getJson
fall through, and catch and report it where it is being called from. Anyway, there is about a 99% chance that as @StevenGoodman suggests the problem is that you are usingawait
outside anasync
function. Please show us the enclosing function. Note that due a recent change in Chrome,await
will be handled properly at the top level in the console, ie even if not in an async function. – user663031 Commented Sep 11, 2017 at 16:01
2 Answers
Reset to default 6Most likely your code looks something like this:
var thingsDone = await Promise.all([
Promise.resolve("eat"),
Promise.resolve("sleep")
]);
console.log(thingsDone);
This will not work because the await
keyword is only valid within an async
function (which the global context is not). It will simply cause a syntax error.
One way to handle this is to use it like a regular old promise and not using the await
keyword:
Promise.all([
Promise.resolve("eat"),
Promise.resolve("sleep")
]).then((thingsDone) => console.log(thingsDone));
Or if you want to get fancy (or need more room to write an expressive function), wrap your logic in an async
function and then handle it like a promise:
async function doThings() {
var eat = await Promise.resolve("eat");
var sleep = await Promise.resolve("sleep");
return Promise.all([Promise.resolve(eat), Promise.resolve(sleep)]);
}
doThings().then((thingsDone) => console.log(thingsDone));
This would allow you to use await
as needed and is much more helpful in a more plicated function.
Or even more succinctly using an immediately-executing async
function:
(async() => {
var eat = await Promise.resolve("eat");
var sleep = await Promise.resolve("sleep");
return Promise.all([Promise.resolve(eat), Promise.resolve(sleep)]);
})().then((thingsDone) => console.log(thingsDone));
torazaburo pointed me to right direction in his ments and i figured it out, this is the final code that is working:
var getJson = async function() {
try {
let response = await fetch('http://mysafeinfo./api/data?list=englishmonarchs&format=json');
let json = await response.json();
return json;
} catch(e) {
console.log('Error!', e);
}
}
var check_all = async function(callback) {
callback( [foo, bar] = await Promise.all([getJson(), getJson()]) );
};
check_all(function(data) {
console.log(data);
});
This works, example here: https://jsfiddle/01z0kdae/1/