externalPromise.js 597 B

1234567891011121314151617181920
  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. const promise = new Promise((res, rej) => {
  10. resolve = res
  11. reject = rej
  12. })
  13. return { resolve, reject, promise }
  14. }
  15. module.exports = {
  16. newExternallyControlledPromise,
  17. }