Ruoxuan.Wang
Ruoxuan.Wang

Reputation: 319

What's the difference between async and sync func in setInterval node.js

I know the setInterval func is to run some periodic task. But I want to know what the difference between async and sync func. Here is an example.

const fetchFile = async ()=>{//dosometing}

setInterval(async()=>{
  fetchFile()
}, 1000)

setInterval(async()=>{
  await fetchFile()
}, 1000)

setInterval(()=>{
  fetchFile()
}, 1000)

Upvotes: 1

Views: 240

Answers (2)

jfriend00
jfriend00

Reputation: 707446

There is no practical difference to the interval itself between any of your three implementations. setInterval() does not pay any attention to the return value from the callback so it doesn't matter at all that the async version returns a promise.

You can declare it async if you want to use await inside the callback for your own implementation, but that won't affect the event loop whether you use await or .then(). With await, as soon as you hit the await on a promise, the callback will return immediately and the event loop will be able to process other events. The same is true if you do fn().then(). While the asynchronous operation is running and the promise is waiting to get notified of completion, the event loop can process other events.

You may also want to know that there's no difference between these two:

fn(async()=>{
  fetchFile()
}, 1000)

fn(async()=>{
  await fetchFile()
}, 1000)

even outside of the setInterval() world. Doing an await on the last line of an async function doesn't add anything. It's just superfluous. But, in the setInterval() case, since setInterval() doesn't pay any attention to the return value from the callback, there's no practical difference between any of your three implementations.

In fact, there's no difference between return fetchFile() and return await fetchFile() either in an async function.


In all your examples, you have NO error handling on fetchFile(). If it rejects, you will have an unhandled rejection. To catch those errors, you would either surround an await with try/catch or use .catch().

Upvotes: 2

Łukasz Szewczak
Łukasz Szewczak

Reputation: 1881

First of all you pass async function callback to setInterval function which not expects async function in result you lack of proper error handling;

So if you want to run some periodic function I propose something like this, where you have more control on your code with error handling. Keep in mind that this could block event loop as your code could run thru microstask queeue only.

const util = require('util');
const sleep = util.promisify(setTimeout);

const fetchFile = async () => {
  console.log('some async function');
};

async function fetchSomeFile() {
  try {
    await fetchFile();
    sleep(1000);
    fetchSomeFile();
  } catch (err) {
    console.log(err);
  }
}

fetchSomeFile().catch(err => console.log(err));

I propose to watch good presentation - Broken Promises - James Snell, NearForm https://youtu.be/XV-u_Ow47s0

Upvotes: 1

Related Questions