externalPromise.js 569 B

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