| 1 |
import { |
| 2 |
noop, |
| 3 |
reject as _reject |
| 4 |
} from '../-internal'; |
| 5 |
|
| 6 |
/** |
| 7 |
`Promise.reject` returns a promise rejected with the passed `reason`. |
| 8 |
It is shorthand for the following: |
| 9 |
|
| 10 |
```javascript |
| 11 |
let promise = new Promise(function(resolve, reject){ |
| 12 |
reject(new Error('WHOOPS')); |
| 13 |
}); |
| 14 |
|
| 15 |
promise.then(function(value){ |
| 16 |
// Code here doesn't run because the promise is rejected! |
| 17 |
}, function(reason){ |
| 18 |
// reason.message === 'WHOOPS' |
| 19 |
}); |
| 20 |
``` |
| 21 |
|
| 22 |
Instead of writing the above, your code now simply becomes the following: |
| 23 |
|
| 24 |
```javascript |
| 25 |
let promise = Promise.reject(new Error('WHOOPS')); |
| 26 |
|
| 27 |
promise.then(function(value){ |
| 28 |
// Code here doesn't run because the promise is rejected! |
| 29 |
}, function(reason){ |
| 30 |
// reason.message === 'WHOOPS' |
| 31 |
}); |
| 32 |
``` |
| 33 |
|
| 34 |
@method reject |
| 35 |
@static |
| 36 |
@param {Any} reason value that the returned promise will be rejected with. |
| 37 |
Useful for tooling. |
| 38 |
@return {Promise} a promise rejected with the given `reason`. |
| 39 |
*/ |
| 40 |
export default function reject(reason) { |
| 41 |
/*jshint validthis:true */ |
| 42 |
let Constructor = this; |
| 43 |
let promise = new Constructor(noop); |
| 44 |
_reject(promise, reason); |
| 45 |
return promise; |
| 46 |
} |