externalPromise.js 700 B

12345678910111213141516171819202122
  1. /**
  2. * Creates a new promise.
  3. * @return { object} Returns an object that contains a Promise and exposes its handlers, ie. resolve and reject methods
  4. * so it can be fulfilled 'externally'. This is a bit of a hack, but most useful application is when
  5. * concurrent async operations are initiated that are all waiting on the same result value.
  6. */
  7. function newExternallyControlledPromise() {
  8. let resolve, reject
  9. // Disable lint until the migration to TypeScript.
  10. // eslint-disable-next-line promise/param-names
  11. const promise = new Promise((res, rej) => {
  12. resolve = res
  13. reject = rej
  14. })
  15. return { resolve, reject, promise }
  16. }
  17. module.exports = {
  18. newExternallyControlledPromise,
  19. }