Last updated: Mar 4, 2024
Reading timeยท4 min

Use the Promise.then() method to access the value of a promise.
The then() method takes a function, which gets passed the resolved value of
the promise as a parameter.
// ๐๏ธ Example promise const p = Promise.resolve('bobbyhadz.com'); p.then(value => { console.log(value); // ๐๏ธ "bobbyhadz.com" }).catch(err => { console.log(err); });

We used the Promise.resolve() method to get an example promise.
We called the then() method on the Promise object to resolve the promise.
The function we passed to the then() method gets called with the resolved
value as a parameter.
We can access this value in the body of the function.
Notice that we also used the catch() method.
This is because a promise can have 3 states:
pending - initial state, neither fulfilled nor rejected.fulfilled - the operation was successful and the promise is resolved.rejected - operation failed.then method will get called if the promise is fulfilled and the operation is successful.If the promise is rejected, we can access the reason (error) for the rejection
in the catch() method.
// ๐๏ธ Example rejected promise const p = Promise.reject('Something went wrong'); p.then(value => { console.log(value); }).catch(err => { console.log(err); // ๐๏ธ "Something went wrong" });

We used the Promise.reject() method to get a rejected promise.
This time, our catch method was invoked and got passed the reason for the
rejection.
The value you return from the function passed to the then() method is wrapped
in a promise, so you can chain as many calls to the method as necessary.
const p = Promise.resolve(13); p.then(value => { console.log(value); // ๐๏ธ 13 return value + value; }).then(value => { console.log(value); // ๐๏ธ 26 return value + value; });

If you don't return a value from the callback function, you implicitly return
undefined, which gets wrapped in a promise and returned by the then()
method.
Alternatively, you can access the value of a promise by using the async/await
syntax.
To access the value of a promise using async/await:
async function.await operator to await the promise.await operator to a variable.// ๐๏ธ example promise const p = Promise.resolve('bobbyhadz.com'); async function example() { try { const value = await p; console.log(value); // ๐๏ธ "Hello World" } catch (err) { console.log(err); } } example();

We can use the await operator because we marked the function as async.
The await operator is used to wait for a Promise to be fulfilled or rejected.
An easy way to think about the await operator is that:
await operator is used, our code in the particular function stops
on the line where await was used until the Promise is fulfilled or rejected.This makes our code much easier to read compared to using the then() method.
Notice that we wrapped the operation in a try/catch block.
This is because, if the promise gets rejected, we want to be able to access the
reason in our catch block.
// ๐๏ธ Example rejected promise const p = Promise.reject('Something went wrong'); async function example() { try { const value = await p; console.log(value); } catch (err) { console.log(err); // ๐๏ธ "Something went wrong" } } example();
We used the Promise.reject() method to get a rejected promise.
When we tried to await the promise, our catch function was called and passed
the reason for the rejection.
There are many use cases where you might want to use the await operator
outside of a function.
// ๐๏ธ Example promise const p = Promise.resolve('bobbyhadz.com'); try { const value = await p; console.log(value); // ๐๏ธ "bobbyhadz.com" } catch (err) { console.log(err); }
The top-level await syntax is available in Node.js if your Node version is
16.12.0 or more recent.
If you intend to use top-level await in the browser, check out the caniuse
table for browser support for
top level await.
At the time of writing, the top-level await operator can be enabled for most
modern browsers by loading the JavaScript file as a module in your HTML file.
<!-- โ Your JS script here โ --> <script type="module" src="index.js"></script>
Notice that we set the type attribute to module.
When using top-level await, it is very important to use a try/catch block
because an uncaught exception caused by a rejected promise might stop your
application from running.
// ๐๏ธ Example rejected promise const p = Promise.reject('Something went wrong'); try { const value = await p; console.log(value); } catch (err) { // ๐๏ธ catch the error here console.log(err); // ๐๏ธ "Something went wrong" }
If a rejection of a promise is not handled, e.g. in a catch block, your server
might be in an inconsistent state.
It's a best practice to use a try/catch block when working with the await
operator.
You can learn more about the related topics by checking out the following tutorials: