u936293
u936293

Reputation: 16264

How to use await in a callback?

My code is:

async function run() {
  await sleep(1);
  fs.readFile('list.txt', 'utf8', function (err, text) {
    console.log(text);
    await sleep(5);
   });
}

run();

await sleep(1) is fine, but await sleep(5) results in:

SyntaxError: async is only valid in async function

Is fs.readFile the problem? How do I use await in such a situation?

The above is a reduced example to test. In my actual usage, I need to put the await sleep in a callback deeply nested inside a few more asynchronous functions.

Upvotes: 4

Views: 822

Answers (4)

Mark
Mark

Reputation: 92461

I'm not sure what sleep is doing, but the problem in your question is happening because the callback to fs.readfile is not an async function and the that is where await sleep(5) is.

async function run() {
    await sleep(1);
    fs.readFile('list.txt', 'utf8', function (err, text) {
    console.log(text);           /* ^this isn't an async function */
    await sleep(5);
    });
}

You could make that error go away by passing an async function as a callback, however mixing promise and callback flows is not recommended and leads to problems, especially when handling errors. It prevents you from chaining the promise and catching errors from the inner await outside of run().

A better approach is to wrap fs.readFile() in a promise and await that.

async function run() {
  await sleep(1);
  const text = await new Promise((resolve, reject) => {
    fs.readFile('list.txt', 'utf8', function (err, text) {
      if (err) reject(err) else resolve(text);
    });
  });
  console.log(text);
  await sleep(5);
}

This will allow you to catch any errors in much more robust way with:

run()
.then(() => /*...*/)
.catch(err => /* handle error */

and avoid unhandled rejections.

Upvotes: 4

Estus Flask
Estus Flask

Reputation: 223064

The above is a reduced example to test. In my actual usage, I need to put the await sleep in a callback deeply nested inside a few more asynchronous functions.

Callback based APIs shouldn't be augmented with async..await or promises in general when there is a chance to stick to promises (may not be possible if a callback is called more than once). This results in poor control flow and error handling.

Once there is async callback function inside run, it's impossible to chain nested promise with run().then(...). Errors from nested promises may remain unhandled as well and result in UnhandledPromiseRejectionWarning.

The proper way is to move from callbacks to promises and use promise control flow. Generally this can be achieved with promise constructor:

async function run() {
  await sleep(1);
  const text = await new Promise((resolve, reject) => {
    fs.readFile('list.txt', 'utf8', function (err, text) {
      if (err) reject(err) else resolve(text);
    });
  });
  console.log(text);
  await sleep(5);
}

There are numerous ways to get promises from Node callback based APIs, e.g. fs provides promise API since Node 10:

async function run() {
  await sleep(1);
  const text = await fs.promises.readFile('list.txt', 'utf8');
  console.log(text);
  await sleep(5);
}

Upvotes: 4

bob
bob

Reputation: 8005

If your function includes an "await", you must prefix your function declaration with "async".

NOTE: By making your function an "async" function a promise is always returned. So if you return a string, that string will get automatically wrapped in a Promise object.

You can therefore return a Promise manually (for clarity). A simple resolved promise is sufficient:

async function foo(){
    await bar();
    return Promise.resolve('optional'); // optionally resolve with a value
}

The keyword "async" always goes before the keyword "function":

var foo = async function(){
    await bar();
    return Promise.resolve('optional'); // optionally resolve with a value
}

... and well for arrow functions:

var foo = sally( async () => {
    await bar();
    return Promise.resolve('optional'); // optionally resolve with a value
})

To retrieve the return value:

foo().then(val => console.log(val) ); // prints "optional" to console

Upvotes: 1

Himanshu Gupta
Himanshu Gupta

Reputation: 440

You have to use aysnc key before function start. await keyword will not work with normal function.

async function run() {
  await sleep(1);
  fs.readFile('list.txt', 'utf8', async function (err, text) {
    console.log(text);
    await sleep(5);
   });
}

run();

Upvotes: 0

Related Questions