UNPKG

790 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createPromiseWithResolver = void 0;
4const doNothing = () => undefined;
5/**
6 * Creates a new promise and exposes its resolver function.
7 * Use with care!
8 */
9function createPromiseWithResolver() {
10 let alreadyResolved = false;
11 let resolvedTo;
12 let resolver = doNothing;
13 const promise = new Promise(resolve => {
14 if (alreadyResolved) {
15 resolve(resolvedTo);
16 }
17 else {
18 resolver = resolve;
19 }
20 });
21 const exposedResolver = (value) => {
22 alreadyResolved = true;
23 resolvedTo = value;
24 resolver(resolvedTo);
25 };
26 return [promise, exposedResolver];
27}
28exports.createPromiseWithResolver = createPromiseWithResolver;