Practice exercise for Promises Async/Await

Exercise 1:

  1. Create a function called getPromise that returns a Promise that resolves after 2 seconds and returns “Resolved!”

  2. Create two new functions that call getPromise within each.

  3. In first function, chain the returned Promise with .then to print resolved value.

  4. In second function, use async and await to handle the returned Promise and printing resolved value.

Comment down your code.

1 Like

function getPromise() {
return new Promise((resolve) => {
setTimeout(() => {
resolve(“Resolved!”);
}, 2000);
});
}

1 Like

1.function getPromise() {
return new Promise((resolve) =>
{
setTimeout(() =>
{
resolve(“Resolved!”);
}, 2000);
});
}

2.function function1() {
getPromise().then((result) => {
console.log(“Function 1:”, result);
});
}
async function function2() {
const result = await getPromise();
console.log(“Function 2:”, result);
}

  1. function function1() {
    getPromise().then((result) => {
    console.log(“Resolved value:”, result);
    });
    }

  2. async function function2() {
    const result = await getPromise();
    console.log(“Resolved value:”, result);
    }

1 Like

Right @balagibadri2002 Well Done !

    1. Create a function called getPromise that returns a Promise that resolves after 2 seconds and returns “Resolved!”
      function getPromise() {
      return new Promise((resolve, reject) => {
      setTimeout(() => {
      resolve(“Resolved!”);
      }, 2000); // Resolves after 2 seconds
      });
      }
  1. Create two new functions that call getPromise within each.

    function getPromise() {
    return new Promise((resolve, reject) => {
    setTimeout(() => {
    resolve(“Resolved!”);
    }, 2000); // Resolves after 2 seconds
    });
    }

function function1() {
return getPromise()
.then((result) => {
console.log(“Function 1:”, result);
})
.catch((error) => {
console.error(error);
});
}

function function2() {
return getPromise()
.then((result) => {
console.log(“Function 2:”, result);
})
.catch((error) => {
console.error(error);
});
}

// Call the functions
function1();
function2();

    1. In first function, chain the returned Promise with .then to print resolved value.

    function function1() {
    return getPromise()
    .then((result) => {
    console.log(“Function 1:”, result); // Print resolved value
    })
    .catch((error) => {
    console.error(error);
    });
    }

function function2() {
return getPromise()
.then((result) => {
console.log(“Function 2:”, result);
})
.catch((error) => {
console.error(error);
});
}

  1. In second function, use async and await to handle the returned Promise and printing resolved value.

function getPromise() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(“Resolved!”);
}, 2000); // Resolves after 2 seconds
});
}

async function function2() {
try {
const result = await getPromise(); // Wait for the Promise to resolve
console.log(“Function 2:”, result); // Print resolved value
} catch (error) {
console.error(error);
}
}

// Call the functions
function2();

And JavaScript is an “asynchronous” and concurrent programming language that offers a lot of flexibility. It’s single-threaded like synchronous but also non-blocking like asynchronous.

1 Like

console.clear();
let getPromise = new Promise(function(resolve, reject) {
return setTimeout(function() { resolve(“Resolved!”); }, 2000);
});

let customer= getPromise
.then (function (result){
console.log(result);
});

async function f() {
let result = await getPromise;
console.log(result);
}

f();

1 Like

Correct! @alagesh732001

That’s correct @meshrampranalee. For additional knowledge, read a litle bit about arrow functions here.