reject.js 1.1 KB

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