Using async requires async function, but my function is async

11,768

Solution 1

You don't read that error message right: the problem isn't the function you're calling but the function you're in.

You may do

(async function(){
    await dbc.solve(img);
    // more code here or the await is useless
})();

Note that this trick should soon enough not be needed anymore in node's REPL: https://github.com/nodejs/node/issues/13209

Solution 2

SyntaxError: await is only valid in async function - just like the error tells you, you may only use await inside a function which is marked as async. So you cannot use the await keyword anywhere else.

https://basarat.gitbooks.io/typescript/docs/async-await.html

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-7.html

examples:

function test() {
  await myOtherFunction() // NOT working
}

async function test() {
  await myOtherFunction() //working
}

You can also make anonymous callback functions async:

myMethod().then(async () => {
  await myAsyncCall()
})

Solution 3

The await operator can only be used in an async function.

Solution 4

You may not need async await abstraction really. Why don't you just simply promisify dbc.solve() function with a promisifier like;

function promisify(f){
  return data => new Promise((v,x) => f(data, (err, id, sol) => err ? x(err) : v({i:id, s:solution})));
}

You will have a promisified version of your dbc.solve() and if it doesn't fire an error you will be returned with an object like {i:id, s: solution} at it's then stage.

Share:
11,768
rodorgas
Author by

rodorgas

Updated on July 21, 2022

Comments

  • rodorgas
    rodorgas almost 2 years

    I'm adapting a library that uses callback to use Promises. It's working when I use then(), but it doesn't work when I use await.

    > dbc.solve
    [AsyncFunction]
    > await dbc.solve(img)
    await dbc.solve(img)
    ^^^^^
    
    SyntaxError: await is only valid in async function
    

    The code for dbc.solve is:

    module.exports = DeathByCaptcha = (function() {
      function DeathByCaptcha(username, password, endpoint) {
        ...
      }
    
      DeathByCaptcha.prototype.solve = async function(img) {
        return new Promise(
          function(resolve, reject) {
            ...
          }
        );
      };
    })();
    

    I believe this has something with the fact solve is member of prototype, but I couldn't find any information about it. I found that node didn't always supported async await for class methods, so I upgraded from node 7, now I'm using node 9.4.0.

  • rodorgas
    rodorgas about 6 years
    Thanks! Isn't the in ambiguous? It says await is only valid in async function, but "function" can be the context or the function which I'm calling.
  • Denys Séguret
    Denys Séguret about 6 years
    @rodorgas It is tricky, most of us have been tricked once, but I don't think it's ambiguous if you're cautious enough. And I don't see any obviously better message.