Promise.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. const Promise = require('promise/setimmediate/es6-extensions');
  12. require('promise/setimmediate/done');
  13. require('promise/setimmediate/finally');
  14. if (__DEV__) {
  15. require('promise/setimmediate/rejection-tracking').enable({
  16. allRejections: true,
  17. onUnhandled: (id, error = {}) => {
  18. let message: string;
  19. let stack: ?string;
  20. const stringValue = Object.prototype.toString.call(error);
  21. if (stringValue === '[object Error]') {
  22. message = Error.prototype.toString.call(error);
  23. stack = error.stack;
  24. } else {
  25. try {
  26. message = require('pretty-format')(error);
  27. } catch {
  28. message = typeof error === 'string' ? error : JSON.stringify(error);
  29. }
  30. }
  31. const warning =
  32. `Possible Unhandled Promise Rejection (id: ${id}):\n` +
  33. `${message}\n` +
  34. (stack == null ? '' : stack);
  35. console.warn(warning);
  36. },
  37. onHandled: id => {
  38. const warning =
  39. `Promise Rejection Handled (id: ${id})\n` +
  40. 'This means you can ignore any previous messages of the form ' +
  41. `"Possible Unhandled Promise Rejection (id: ${id}):"`;
  42. console.warn(warning);
  43. },
  44. });
  45. }
  46. module.exports = Promise;