- asynchronous programming
- handle multiple tasks at the same time
- kip the current operation and move to the next line of the code
Syntax
var promise = new Promise(function(resolve, reject){
// our logic goes here ..
});
- If the function’s response is a success, then it will return ‘resolve’
-
If the response from the function is not successful, it will return ‘reject’.
States
- reject: If the response from the promise function fails, then the state would be ‘reject’.
- pending: We the response does not come, and we are waiting for the result, then the state would be ‘pending’.
- fulfilled: If the response forms the promise in TypeScript is received successfully, then the state would be ‘fullfilled’.
Error
use reject param
handle with a catch block
function demo() {
var promise = new Promise((resolve, reject) => {
// our logic goes here ..
reject();
}
demo().then(function(success) {
// success logic will go here ..
})
.catch(function(error) {
// logic goes here //
});