0% found this document useful (0 votes)
22 views

Promises: Jogesh K. Muppala

Promises represent the eventual completion (or failure) of an asynchronous operation, and are used to provide a mechanism for asynchronous computation. A promise is created in a pending state, and may be fulfilled with a value at some point in the future, or rejected with a reason for failure. Promises support chaining of operations through then and catch methods, avoiding nested callbacks and making asynchronous code more readable.

Uploaded by

vikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Promises: Jogesh K. Muppala

Promises represent the eventual completion (or failure) of an asynchronous operation, and are used to provide a mechanism for asynchronous computation. A promise is created in a pending state, and may be fulfilled with a value at some point in the future, or rejected with a reason for failure. Promises support chaining of operations through then and catch methods, avoiding nested callbacks and making asynchronous code more readable.

Uploaded by

vikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Promises

Jogesh K. Muppala
Promise
• Promise is a mechanism that supports
asynchronous computation
• Proxy for a value not necessarily known when the
promise is created:
– It represents a value that may be available now, or in
the future, or never

2
Promise
Promise
(pending)

resolve/fulfill reject

Promise Promise
(resolved/fulfilled) (rejected)

new Promise ( function (resolve, reject) { . . . } );

3
Promise Example
. . . getDishes(): Promise<Dish[]> {
getDishes()
return new Promise (
function(resolve, reject) {
.then ( function(result) {
// do something
if (successful) {
} ) resolve(result);
}

.catch ( function(error) { else {


reject(error);
}

} ); });
}

4
Why Promises?
• Solves the callback hell (heavily nested callback
code) problem
• Promises can be chained
• Can immediately return:
– Promise.resolve(result)
– Promise.reject(error)

You might also like