promise-adapter.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2015-present, Vitaly Tomilov
  3. *
  4. * See the LICENSE file at the top-level directory of this distribution
  5. * for licensing information.
  6. *
  7. * Removal or modification of this copyright notice is prohibited.
  8. */
  9. const {assert} = require('./assert');
  10. /**
  11. * @class PromiseAdapter
  12. * @summary Adapter for the primary promise operations.
  13. * @description
  14. * Provides compatibility with promise libraries that cannot be recognized automatically,
  15. * via functions that implement the primary operations with promises:
  16. *
  17. * - construct a new promise with a callback function
  18. * - resolve a promise with some result data
  19. * - reject a promise with a reason
  20. * - resolve an array of promises
  21. *
  22. * The type is available from the library's root: `pgp.PromiseAdapter`.
  23. *
  24. * @param {object} api
  25. * Promise API configuration object.
  26. *
  27. * Passing in anything other than an object will throw {@link external:TypeError TypeError} = `Adapter requires an api configuration object.`
  28. *
  29. * @param {function} api.create
  30. * A function that takes a callback parameter and returns a new promise object.
  31. * The callback parameter is expected to be `function(resolve, reject)`.
  32. *
  33. * Passing in anything other than a function will throw {@link external:TypeError TypeError} = `Function 'create' must be specified.`
  34. *
  35. * @param {function} api.resolve
  36. * A function that takes an optional data parameter and resolves a promise with it.
  37. *
  38. * Passing in anything other than a function will throw {@link external:TypeError TypeError} = `Function 'resolve' must be specified.`
  39. *
  40. * @param {function} api.reject
  41. * A function that takes an optional error parameter and rejects a promise with it.
  42. *
  43. * Passing in anything other than a function will throw {@link external:TypeError TypeError} = `Function 'reject' must be specified.`
  44. *
  45. * @param {function} api.all
  46. * A function that resolves an array of promises.
  47. *
  48. * Passing in anything other than a function will throw {@link external:TypeError TypeError} = `Function 'all' must be specified.`
  49. *
  50. * @returns {PromiseAdapter}
  51. */
  52. class PromiseAdapter {
  53. constructor(api) {
  54. if (!api || typeof api !== 'object') {
  55. throw new TypeError('Adapter requires an api configuration object.');
  56. }
  57. api = assert(api, ['create', 'resolve', 'reject', 'all']);
  58. this.create = api.create;
  59. this.resolve = api.resolve;
  60. this.reject = api.reject;
  61. this.all = api.all;
  62. if (typeof this.create !== 'function') {
  63. throw new TypeError('Function \'create\' must be specified.');
  64. }
  65. if (typeof this.resolve !== 'function') {
  66. throw new TypeError('Function \'resolve\' must be specified.');
  67. }
  68. if (typeof this.reject !== 'function') {
  69. throw new TypeError('Function \'reject\' must be specified.');
  70. }
  71. if (typeof this.all !== 'function') {
  72. throw new TypeError('Function \'all\' must be specified.');
  73. }
  74. }
  75. }
  76. module.exports = {PromiseAdapter};