DEV Community

Sathish K
Sathish K

Posted on

In built asynchronous types in javaScript

Name the the types of asynchronous ?
setTimeout().
setInterval().
Promise.
Async and Await.

What is the setTimeout() ?
setTimeout is a asynchronous operation in javascript,the function of code is schedules the execution of the function is delay without the block.

console.log("1");

setTimeout(() => {
  console.log("2");
}, 1000);

console.log("3");



**Output**

1
3
2   ← after ~1 second

Enter fullscreen mode Exit fullscreen mode

** what is the setInterval() in asynchronous ?**
setInterval() is asynchronous method used to code block at fixed time interval.
In continous to run or live time untill and its a callback.

const id = setInterval(() => {
  console.log("Running...");
}, 1000);

setTimeout(() => {
  clearInterval(id);
  console.log("Stopped");
}, 5000);

Enter fullscreen mode Exit fullscreen mode

What is the Promise ?
Promise is the object represent the eventual completion or failure.
It is easily understand the code and simplify the code.
It is a two type resolve, reject.

what is the Async/Await ?
Built on top of Promises, async/await (introduced in ES2017) offers a more synchronous-looking syntax for writing asynchronous code, making it highly readable and easier to manage, especially in complex scenarios.

Top comments (0)