0% found this document useful (0 votes)
33 views4 pages

JavaScript Synchronous vs Asynchronous Code

The document discusses synchronous and asynchronous JavaScript code examples including setTimeout, setInterval, promises, and async/await. It shows how promises and async/await can be used to handle asynchronous operations.

Uploaded by

patilniraj277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

JavaScript Synchronous vs Asynchronous Code

The document discusses synchronous and asynchronous JavaScript code examples including setTimeout, setInterval, promises, and async/await. It shows how promises and async/await can be used to handle asynchronous operations.

Uploaded by

patilniraj277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//synchronous:

// [Link]("hi");
// [Link]("hello");

// function myFirst() {
// [Link]("hello");
// }
// function mySecond() {
// [Link]("world");
// }

// mySecond();
// myFirst();

//asynchronous:
// [Link]("hi") //hi

// setTimeout(()=> {
// [Link]("world")
// }, 3000) //after 3 second it
will print

// [Link]("hello") // hello
// setInterval(() => {
// let d = new Date();
// [Link]([Link]()+":"+[Link]
tMinutes()+":"+[Link]());
// },3000);

// //Promise Object
// const myPromise = new Promise((res,
rej) => {
// setTimeout(() => {
// res("done");
// }, 3000);
// });

// [Link](function (value) {
// [Link](value);
// });

// // if true then it prints success


// let ans = new Promise((res, rej) =>
{
// if (true) {
// return res();
// } else {
// return rej();
// }
// });
// ans
// .then(function () {
// [Link]("success");
// })
// .catch(() => {
// [Link]("fall");
// });

// // if false then it prints fall


// let ans1 = new Promise((res, rej)
=> {
// if (false) {
// return res();
// } else {
// return rej();
// }
// });
// ans1
// .then(function () {
// [Link]("success");
// })
// .catch(() => {
// [Link]("fall");
// });

//async and await

async function abcd() { //async return


a promise
let result = await
fetch("[Link]
");//await waits for a promise
let data = await [Link]();
[Link](data);
}
abcd();

You might also like