2. Asynchronous Programming
2. Asynchronous Programming
What are Promises?
- We previously mentioned that
callback
functions are used when working with asynchronous programming, however that’s not the only method - Promises are the more modern way to work with asynchronous code
- A
Promise
is an object which represents the eventual failure or completion of an asynchronous operation - It represents that eventual result with a few states:
- Pending: A penidng promise represents an asynchronous operation which is still ongoing, and doesn’t have a concrete result yet
- Fulfilled: The asynchronous operation has completed successfully, and a result is available
- Rejected: The asynchronous operation has failed, and an error is available
Creating a Promise
- The main way to create a
Promise
is with thePromise
constructor - The constructor takes a function with two arguments:
resolve
: This function should be called when thePromise
succeedsreject
: This function should be called if thePromise
fails
- Factory methods also exist to simplify this process, including:
Promise.resolve
which takes a value as a parameter and returns a fulfilledPromise
with the provided value as the resultPromise.reject
which takes an error as a parameter and returns a rejectedPromise
with the provided error available
Using promises
- There are three main methods used to handle the result of a
Promise
they are:then()
: Runs after a promise is fulfilled.then
takes one parameter, a callback function which is ran with the result of thePromise
as a parametercatch()
: Runs if a promise is rejected.catch
takes one parameter, a callback function which is ran with the error that is thrown when thePromise
is rejectedfinally()
: Runs last, after thethen
orcatch
operation, and runs regardless of whether or not thePromise
is rejected or fulfilled.finally
takes a callback function as a parameter
- Each of these methods returns a new
Promise
upon which, the above methods can be used
Chaining Promises
As mentioned earlier, each call to
then
,catch
orfinally
returns a newPromise
objectThis allows you to chain Promises together
Example:
const myPromise = Promise.resolve("Hello"); myPromise .then((result) => { console.log(result); return "World"; }) .then((result) => { console.log(result); });
- This would print:
Hello World
Async/Await
async
andawait
are syntactic sugar built on top of Promises- They allow you to write
asynchronous
code that looks like synchronous code, so it can be easier to reason about - Examples of
async/await
:- Assume we have the following function to fetch data:
function fetchData() { return new Promise((resolve, reject) => { const success = true; if (success) { resolve('Data fetched successfully'); } else { reject('Error fetching data'); } }); }
- If we wanted to safely call it with the usual Promise syntax, we could write something like:
fetchData() .then((data) => console.log(data)); .catch((error) => console.error(error));
- That same code could be written like the following using
async/await
try { const data = await fetchData(); console.log(data); } catch (error) { console.error(error); }
- Assume we have the following function to fetch data: