简体   繁体   English

Java语句在if语句中等待保留关键字错误

[英]Javascript Await reserved keyword error inside if statement

I am writing a small JS script where a user enters his IBAN and after entering the first 12 characters I am requesting the bank name and BIC from a different URL. 我正在写一个小的JS脚本,用户在其中输入IBAN,然后输入前12个字符后,我从另一个URL请求银行名称和BIC。 As I have to wait for this request to finish, I made my function asynchronous and added an 'await' to the request. 由于必须等待此请求完成,因此使函数异步,并在请求中添加了“ await”。 However, my script is no longer compiling with the following Error: 'Parsing error: await is a reserved word' pointing to my newly added 'await'. 但是,我的脚本不再编译并出现以下错误:“解析错误:await是保留字”,指向我新添加的“ await”。 As this 'await' is inside several if conditions, i figured this might be the problem. 由于这种“等待”是在多个if条件内,因此我认为这可能是问题所在。 However, it needs to be inside these if conditions. 但是,如果有条件,它必须位于这些内部。 So does anyone has an idea what I am doing wrong and how to get the await inside the if condition? 那么,有没有人知道我在做什么错,以及如何在if条件下等待?

This is my code: 这是我的代码:

function requestBICandBankname(country, bban) {
  return new Promise(function (resolve, reject) {
    const xhr = new XMLHttpRequest();
    const url = URL + country.toUpperCase() + '/' + bban;

    xhr.open('GET', url, true);
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        return resolve(xhr.response);
      }
      return reject({
        status: this.status,
        statusText: xhr.statusText
      });

    };
    xhr.onerror = function () {
      return reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    xhr.send();

  });
}

async function initLookup() {
  const ibanData = document.getElementById('iban-details');
  let country = '';
  let bban;

  if (ibanData !== null) {
    ibanData.oninput = function (event) {
      const iban = ibanData.value;

      if (iban.length === 12) {
        country = iban.substr(0, 2);
        bban = iban.substr(4, 12);
        try {
          const bankdata = await requestBICandBankname(country, bban);
          console.log('bankdata', bankdata);
        } catch (err) {
          console.log(err);
        }
      }
    };
  }

};

export {
  initLookup
};

JSFiddle JSFiddle

You can only await inside a function that is declared as async . 您只能在声明为async的函数中await

The ibanData.oninput function is not declared that way. 不会以这种方式声明ibanData.oninput函数。

You can convert the event as a promise but should listen to an error as well (if that exists) or it'll never continue if it fails in some way: 您可以将事件转换为Promise,但也应该侦听错误(如果存在),否则如果以某种方式失败则永远不会继续:

const inputAsPromise = (ibanData) =>
  new Promise((resolve, reject) => {
    ibanData.oninput = resolve;
    //@todo: onerror should be reject
  });
async function initLookup() {
  const ibanData = document.getElementById('iban-details');
  let country = '';
  let bban;

  if (ibanData !== null) {
    await inputAsPromise(ibanData);
    const iban = ibanData.value;

    if (iban.length === 12) {
      country = iban.substr(0, 2);
      bban = iban.substr(4, 12);
      try {
        const bankdata = await requestBICandBankname(
          country,
          bban,
        );
        console.log('bankdata', bankdata);
      } catch (err) {
        console.log(err);
      }
    }
  }
}

The problem with the code above is that oninput probably fires multiple times but you can only resolve the promise once. 上面代码的问题是oninput可能会触发多次,但是您只能解决一次oninput If that is the case then you can resolve it only if certain conditions are met: 如果是这种情况,那么只有在满足某些条件的情况下,您才能解决它:

const inputAsPromise = (ibanData) =>
  new Promise((resolve, reject) => {
    ibanData.oninput = (e)=>ibanData.value.length === 12?resolve():'';
    //@todo: onerror should be reject
  });

That was just an example, you have to see what would work in your case. 那只是一个例子,您必须查看适用于您的情况的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM