最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - await Promises.all SyntaxError - Stack Overflow

programmeradmin1浏览0评论

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

Share Improve this question edited Sep 11, 2017 at 15:47 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked Sep 11, 2017 at 15:37 MeviaMevia 1,5642 gold badges20 silver badges56 bronze badges 10
  • 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 an async function? – Steven Goodman Commented Sep 11, 2017 at 15:43
  • 2 Could you post the code that utilizes Promise.all itself? If getJson is working well, it's most likely something with how you're calling Promise.all or the context you're calling it from. – Steven Goodman Commented Sep 11, 2017 at 15:51
  • 2 You need to return response from getJson, 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 the catch in getJson, 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 using await outside an async 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
 |  Show 5 more ments

2 Answers 2

Reset to default 6

Most 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/

发布评论

评论列表(0)

  1. 暂无评论