Mark As Completed Discussion

The output of this next bit of code will be Promise { ‘Hello’ }. Even though we are returning a string it is wrapped in a promise. How do we access this data from the promise?

There are three methods that are attached to a promise, they are .then(), .catch() and .finally(). They are used to access the data from the promise. The first method is called after a promise is resolved, the second one is called if a promise is rejected and the last one always gets called (used for closing connections, clean-up, etc). Let’s wrap all of this into a nice little example.

JAVASCRIPT
1hello().then(value => console.log(value));

This will give the output as Hello. In the same way, if we encounter an error, the output can be retrieved by using the .catch() method.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment