Skip to content
the poor coder

How to access return value of promise

Poor Man
Published
Reading time
2 min read
On this page

How to Access Return Value of Promise#

Promises are a way of handling asynchronous operations in JavaScript. They are used to handle operations that may take time to complete and return a value at some point in the future. Once the promise is resolved, it returns the value. To access the return value of a promise, we need to wait for the promise to resolve.

Using .then() Method#

The most common way to access the return value of a promise is by using the .then() method. This method is called on the promise object and takes a function as an argument. This function will be called with the resolved value of the promise.

Text

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("Hello, world!");
  }, 1000);
});

myPromise.then((result) => {
  console.log(result);
});

In this example, we create a new promise that resolves with the string "Hello, world!" after 1 second. We then call the .then() method on the promise object and pass in a function that logs the result to the console.

Using async/await Syntax#

Another way to access the return value of a promise is by using the async/await syntax. This syntax allows us to write asynchronous code that looks like synchronous code.

Text

async function myFunction() {
  const result = await myPromise;
  console.log(result);
}

myFunction();

In this example, we define an async function called myFunction() that awaits the resolved value of myPromise. The value is then logged to the console.

Using .catch() Method#

If the promise rejects with an error, we can access the error message by using the .catch() method. This method is called on the promise object and takes a function as an argument. This function will be called with the error message.

Text

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject("Oops, something went wrong!");
  }, 1000);
});

myPromise.catch((error) => {
  console.log(error);
});

In this example, we create a new promise that rejects with the string "Oops, something went wrong!" after 1 second. We then call the .catch() method on the promise object and pass in a function that logs the error to the console.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC