Promises give us a way to make sense out of asynchronous behavior. When making an asynchronous request, one of two things can happen: everything goes as we hope or there’s an error. This can happen in a number of different ways.
![]() |
| Promise in JavaScript | JavaScript |
For example, we could try several ways to obtain the data to reach success. We also could receive mul‐ tiple types of errors. Promises give us a way to simplify back to a simple pass or fail.
Example 1: If student pass then promise resolve and return .then
var myPromise = new Promise(function (resolve, reject) {
const x = 'Pass'
if (x == 'Pass') {
resolve()
} else {
reject()
}
})
myPromise
.then(function () {
console.log('OMG!, you also pass the exam. Lelo bete mobile lelo')
})
.catch(function () {
console.log('Hufffff! Mujhe to pata that ')
})
//OMG!, you also pass the exam. Lelo bete mobile lelo
