skip to Main Content

I have declared the async before function,but it still notice that The ‘await’ operator can only be used in an ‘async’ function

async function Refresh(regListResult) {
    $(".dfxContent").each(function (index) {
        if (index >= 13 && index < 37) {
            var regComment = $(this).nextAll().eq(4).html();
            if (-1 !== regComment.indexOf("1")) {
                // 在这里加一个读通道位置的函数
                var chnPlace = ChnPlaceGet(a, b, c);
                await new Promise(resolve => setTimeout(resolve, 1000));
                var chnPlaceData = '两者应匹配,' + chnPlace;
                RefreshAnother(Data, index);
                $(this).html(a + b + c);
            }
        }
    });
}

How can I make the statement valid?

3

Answers


  1. Perhaps you mean something closer to this

    async function Refresh(regListResult) {
      const promises = $(".dfxContent").map(async function(index) { // jQuery "map"
        if (index >= 13 && index < 37) {
          var regComment = $(this).nextAll().eq(4).html();
          if (regComment.indexOf("1") !== -1) {
            var chnPlace = await ChnPlaceGet(a, b, c); // Assuming ChnPlaceGet is async
            await new Promise(resolve => setTimeout(resolve, 1000));
            var chnPlaceData = '两者应匹配,' + chnPlace;
            await RefreshAnother(Data, index); // Assuming RefreshAnother is async too
            $(this).html(a + b + c);
          }
        }
      }).get(); // convert the jQuery "map" to an array of promises
    
      // Wait for all promises to resolve
      await Promise.all(promises);
    }
    
    Login or Signup to reply.
  2. Your error is because you didn’t declare the async function correctly. To fix this error you have to declare the async to right first function which includes await keywords.

    So please change

    $(".dfxContent").each(function (index) {...}
    

    to

    $(".dfxContent").each(async function (index) {...} 
    

    and try again.

    Login or Signup to reply.
  3. each is expecting an async function. But each is not awaited, so is better if you map Promises, and you wait them after:

    async function Refresh(regListResult) {
            const promises = $(".dfxContent").map((el, index) => {
                return new Promise((resolve, reject) => {
                    if (index >= 13 && index < 37) {
                        var regComment = $(el).nextAll().eq(4).html();
                        if (-1 !== regComment.indexOf("1")) {
                            // 在这里加一个读通道位置的函数
                            var chnPlace = ChnPlaceGet(a, b, c);
                            setTimeout(() => {
                                var chnPlaceData = '两者应匹配,' + chnPlace;
                                RefreshAnother(Data, index);
                                $(el).html(a + b + c);
                                resolve()
                            }, 1000);                                        
                        }
                    }
                })        
            }).get();
            await Promise.all(promises)
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search