Skip to main content
InterviewHubPRO
Section Overview
Q359

Question 359 of 536

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

67%
JavaScript & V8 Internals (536 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

What are asynchronous thunks

senior LevelCode ExampleAsync & PromisesEvent LoopHTTP & REST APIs

The asynchronous thunks are useful to make network requests. Let's see an example of network requests,

javascript
  function fetchData(fn) {
    fetch("https://jsonplaceholder.typicode.com/todos/1")
      .then((response) => response.json())
      .then((json) => fn(json));
  }

  const asyncThunk = function () {
    return fetchData(function getData(data) {
      console.log(data);
    });
  };

  asyncThunk();

The getData function won't be called immediately but it will be invoked only when the data is available from API endpoint. The setTimeout function is also used to make our code asynchronous. The best real time example is redux state management library which uses the asynchronous thunks to delay the actions to dispatch.