语法错误:意外的保留字"await",node.js是正确的版本 [英] SyntaxError: Unexpected reserved word "await", node.js is correct version

查看:355
本文介绍了语法错误:意外的保留字"await",node.js是正确的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图异步运行一个函数20次.我有函数定义:

I am trying to run a function asynchronously 20 times. I have the function definition:

import axios from 'axios';

async function updateUser (url, user) {
        return new Promise((resolve, reject) => {
            axios.put(url, JSON.stringify(user))
                .then(response => {
                //Folio returned code 204
                console.log('The user has been updated');
                resolve(response.data);
                }).catch((err) => {
                //Folio returned error
                console.error(`Error Text: ${err.response.data}`);
                console.error(`Error Code: ${err}`);
                });
        });
    };

  export {updateUser};

然后,我尝试在外部JSON中循环访问用户,并一次将其更新到我的系统20:

Then I am trying to loop through users in an external JSON, and update them to my system 20 at a time:

import {updateUser} from './updateUser.js';
import usersjson from './jsons/users.json';
let promises=[];
if (usersjson.users.length) {
    try {
        for (const user of usersjson.users) {
            update = new promise ((resolve,reject) => {
                updateUser(url, user)
                .then((list) => {resolve(list)})
                .catch((error)=>{reject(error)})
            });
            promises.push(update);
            if (promises.length = 20) {
                await Promise.all(promises)
                    .then((responses)=> {
                      console.log(responses);
                      promises=[];
                    }).catch((err)=> {
                      console.log(err);
                    });
            }
        }
    } catch(err)=> {
        console.log(err);
    }
}

但是,我在控制台中收到一条错误消息:

However, I am getting an error message in the console:

await Promise.all(promises)
^^^^^

SyntaxError: Unexpected reserved word

我的节点版本为12.19.0(根据先前的问题,看来它必须至少为10,所以这不应该成为问题).

My node version is 12.19.0 (based on previous questions, it seems it has to be at least 10, so that shouldn't be the issue).

当我在不等待的情况下运行该功能时,它们可以正常工作(即,用户已在我的系统上更新).似乎for循环本身是异步的,但是我担心如果同时有太多调用,我的系统将阻塞API,所以我想一次最多进行20-50次调用.

When I run the function without await, they work correctly (i.e. the users are updated on my system). It also seems that the for loop is asynchronous by itself, but I'm afraid that if there will be too many calls simultaneously my system will block the API, so I want to make 20-50 calls at a time, tops.

推荐答案

要使用 await ,您需要声明将 await 放入其中的函数异步

In order to use await you need to declare the function in which you are putting the await as async

例如:

const functionReturningPromise = (input) => {
  return new Promise((resolve, reject) => {
    if (!input) {
      return reject();
    }
    
    return resolve();
  });
}

const functionAwaitingPromise = async () => {
  for (let i = 0; i < 20; i +=1) {
    try {
      await functionReturningPromise(i);
      
      console.log(i);
    } catch (error) {
      console.log(error);
    }
  }
}

这篇关于语法错误:意外的保留字"await",node.js是正确的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆