| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 |
|
| 12 | function getPromiseResult(promise) {
|
| 13 | if (promise.status === "fulfilled") return {
|
| 14 | status: "fulfilled",
|
| 15 | value: promise.value
|
| 16 | };
|
| 17 | if (promise.status === "rejected") return {
|
| 18 | status: "rejected",
|
| 19 | reason: promise.reason
|
| 20 | };
|
| 21 | }
|
| 22 | let awaitPromises = new WeakMap();
|
| 23 | function getAwaitPromise(source, onError) {
|
| 24 | let cached = awaitPromises.get(source);
|
| 25 | if (!cached) {
|
| 26 | cached = {
|
| 27 | promise: Promise.resolve(source).then((value) => settle({
|
| 28 | status: "fulfilled",
|
| 29 | value
|
| 30 | }), (reason) => {
|
| 31 | let result = settle({
|
| 32 | status: "rejected",
|
| 33 | reason
|
| 34 | });
|
| 35 | try {
|
| 36 | cached.onError(reason);
|
| 37 | } catch (error) {
|
| 38 | console.error("<Await> caught the following error in onError", error);
|
| 39 | }
|
| 40 | return result;
|
| 41 | }),
|
| 42 | onError
|
| 43 | };
|
| 44 | awaitPromises.set(source, cached);
|
| 45 | } else cached.onError = onError;
|
| 46 | function settle(result) {
|
| 47 | Object.assign(cached.promise, {
|
| 48 | status: "fulfilled",
|
| 49 | value: result
|
| 50 | });
|
| 51 | return result;
|
| 52 | }
|
| 53 | let result = getPromiseResult(source);
|
| 54 | if (result) settle(result);
|
| 55 | return cached.promise;
|
| 56 | }
|
| 57 |
|
| 58 | export { getAwaitPromise, getPromiseResult };
|