There are two ways to return a promise from a function.
- Using the constructor:
const isGreaterThanTen = number => {
return new Promise( (resolve, reject) => {
return number > 10
? resolve(number)
: reject(Error('this number is TOO SMALL'));
});
}
isGreaterThanTen(12)
.then( /* will be called */ )
.catch( /* not called */ )
- Using the resolve/reject methods:
const isGreaterThanTen = number => {
return number > 10
? Promise.resolve(number)
: Promise.reject(Error('this number is TOO SMALL'));
}
isGreaterThanTen(5)
.then( /* not called */ )
.catch( /* will be called */ )
Try to use the methods when possible, but if you are using a callback inside the function (such as with XMLHttpRequest), you will have to use the constructor.
- Open
index.html
in your browser. - Finish the methods in
functions.js
to pass the tests intests.js
!