Callbacks, Promises, and Async-Await

  1. Callbacks
  2. Promises
  3. Async-Await

Callbacks

Source: Cloudinary
console.log("Hello,"); //Instruction 1const asyncExample = (givenName) => {setTimeout(() => {return { name: givenName };}, 3000);};console.log(asyncExample("I am Puja")); //Instruction 2console.log("How are you?"); //Instruction 3
Hello,
undefined
How are you?
console.log("Hello,"); //Instruction 1const asyncExample = (givenName,callback) => {setTimeout(() => {callback( { name: givenName });}, 3000);};const names=asyncExample("I am Puja",names=>{
console.log(names)
}); //Instruction 2
console.log("How are you?"); //Instruction 3
Hello,
How are you?
I am Puja
Source: Thapa Technical
const nameInfo = (firstName, secondName, callback) => {setTimeout(() => {callback({ fname: firstName, sname: secondName });}, 3000);};const hobbiesInfo = (firstName, callback) => {setTimeout(() => {callback(["Cooking", "Swimming", "Movies"]);}, 3000);};const favMovies = (hobby, callback) => {setTimeout(() => {callback(["Arrival", "Interstellar", "Shutter Island"]);}, 3000);};const names = nameInfo("Puja", "Chaudhury", (names) => {hobbiesInfo(names.fname, (hobbies) => {favMovies(hobby[0], (movies) => {console.log(movies[0]);});});});

Promises

Source: Freecodecamp
const promise = new Promise((resolve, reject) => {setTimeout(() => {resolve({ fname: "Puja" });}, 2000);});promise.then((name) => {console.log(name.fname);});
Puja
const nameInfo = (firstName, secondName) => {return new Promise((resolve, reject) => {setTimeout(() => {resolve({ fname: firstName, sname: secondName });}, 3000);});};const hobbiesInfo = (firstName) => {return new Promise((resolve, reject) => {setTimeout(() => {resolve(["Cooking", "Swimming", "Movies"]);}, 3000);});};
const favMovies = (hobby) => {return new Promise((resolve, reject) => {setTimeout(() => {resolve(["Arrival", "Interstellar", "Shutter Island"]);}, 3000);});};nameInfo("Puja", "Chaudhury").then((names) => hobbiesInfo(names)).then((hobbies) => hobbiesInfo(hobbies)).then((movies) => favMovies(movies)).then((movie) => console.log(movie[0]));
Promise.all(async1,async2)
.then(result=>console.log(result));

Async-Await

Source: Frontend Weekly
const nameInfo = (firstName, secondName) => {return new Promise((resolve, reject) => {setTimeout(() => {resolve({ fname: firstName, sname: secondName });}, 3000);});};const hobbiesInfo = (firstName) => {return new Promise((resolve, reject) => {setTimeout(() => {resolve(["Cooking", "Swimming", "Movies"]);}, 3000);});};const favMovies = (hobby) => {return new Promise((resolve, reject) => {setTimeout(() => {resolve(["Arrival", "Interstellar", "Shutter Island"]);}, 3000);});};
//Syncasync function userInfo() {const names = await nameInfo("Puja", "Chaudhury");const hobbies = await hobbiesInfo(names.fname);const movies = await favMovies(hobbies[0]);console.log(movies[0]);}userInfo();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Puja Chaudhury

Puja Chaudhury

45 Followers

HI I am Puja. Developer and Electrical engineer in the making with a passion for writing!