How to access the Value of a Promise in JavaScript Last Updated : 17 Dec, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to access the value of Promise in JavaScript. The Promise is a feature of ES6 introduced in 2015. The concept of Promises is generally used when we want to work asynchronously. The value of a Promise can be accessed in JavaScript using the following methods. Table of Content Using .then( ) MethodUsing the Async/Await MethodSyntax: let promise = new Promise((resolve, reject)=>{//enter the code})Approach: Using .then( ) MethodIn this code example, we have created a variable createPromise which is a promise having two callbacks provided by javascript.resolve: when the promise resolves successfully.reject: when the promise is rejected for any reason.Then creating setTimeout has a callback function that will run after two seconds.By using the .then( ) method access the value of the Promise. Example: The code example shows how to access the value of a Promise in JavaScript using the .then method. JavaScript console.log("wait for 2 seconds to get promise resolved") const createPromise = new Promise( (resolve, reject) => { setTimeout(function () { resolve( "the promise is resolved after 2 seconds"); }, 2000) }); createPromise.then((promisedata) => { console.log("Promise is resolved successfully", promisedata) }) Output: access the Value of a Promise in JavaScript Using .then MethodApproach 2: Using the Async/Await MethodAn async function looks like simple JavaScript function but it has the feature of working with await expressions. promisefun is a async function and in the body of that function there is an await expression that waits promise to resolve once it resolved the user access the value of the promise.We have used the setTimeout and in callback function we have resolve the promise with the string after two seconds.Then, call the async function as promiseFun().Example: The example show access the value of a Promise in JavaScript using async/await method. JavaScript console.log("wait for 2 seconds to get promise resolved") async function promiseFun() { const createPromise = new Promise( (res, rej) => { setTimeout(function () { res( "the promise resolved"); }, 2000) }); const waitPromise = await createPromise console.log(waitPromise) } promiseFun() Output: Output Comment More infoAdvertise with us Next Article How to access the Value of a Promise in JavaScript S shivanigupta18rk Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to access the value of a Promise in AngularJS ? AngularJS is one of the JS frameworks that consists of promises that are used to handle asynchronous tasks. In some scenarios, we need to handle the promise values by accessing them in the application. So, this can be done using the .then() method and also by using the callbacks in AngularJS. In thi 4 min read How to Convert Callback to Promise in JavaScript ? Asynchronous programming in JavaScript often involves the use of callbacks. However, callbacks can lead to callback hell and make the code harder to read and maintain. Promises provide a cleaner way to handle asynchronous operations. Converting existing callback-based code to use promises can improv 2 min read How to run a given array of promises in series in JavaScript ? Given an array of Promises, we have to run that in a series. To do this task, we can use then(), to run the next promise, after the completion of a promise. Approach: The then() method returns a Promise, which helps us to chain promises/methods. The Promise.resolve() method executes the first callba 3 min read JavaScript - How to Handle Errors in Promise.all? To handle errors in Promise.all(), you need to understand that it fails immediately if any of the promises reject, discarding all resolved values. This behavior can lead to incomplete operations and potential application crashes. The best way to manage errors in Promise.all() is by using .catch() in 3 min read How to Implement a Custom Promise in JavaScript ? A custom Promise in JavaScript encapsulates asynchronous operations. It takes two parameters, resolves and rejects functions, and executes an async task. Resolve is called on success with the result, while reject is called on failure with an error. Table of Content Using Class SyntaxUsing Factory Fu 3 min read Like