0% found this document useful (0 votes)
19 views

Synchronous, Asynchronous, Promise Object, Async, Await

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Synchronous, Asynchronous, Promise Object, Async, Await

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//synchronous:

// console.log("hi");
// console.log("hello");

// function myFirst() {
// console.log("hello");
// }
// function mySecond() {
// console.log("world");
// }

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

//asynchronous:
// console.log("hi") //hi

// setTimeout(()=> {
// console.log("world")
// }, 3000) //after 3 second it
will print

// console.log("hello") // hello
// setInterval(() => {
// let d = new Date();
// console.log(d.getHours()+":"+d.ge
tMinutes()+":"+d.getSeconds());
// },3000);

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

// myPromise.then(function (value) {
// console.log(value);
// });

// // if true then it prints success


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

// // if false then it prints fall


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

//async and await

async function abcd() { //async return


a promise
let result = await
fetch("https://dummyjson.com/products/
");//await waits for a promise
let data = await result.json();
console.log(data);
}
abcd();

You might also like