Skip to content

Commit 4c4fd6d

Browse files
committed
refactor: resolve promises with Promise.all
1 parent 7890b78 commit 4c4fd6d

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

Diff for: blocks/flag.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import JsonViewer from "../components/JsonViewer.tsx";
55
import type { Block, BlockModule, InstanceOf } from "../engine/block.ts";
66
import { isDeferred } from "../engine/core/resolver.ts";
77
import { type Device, deviceOf } from "../utils/userAgent.ts";
8-
import { isAwaitable } from "../utils/mod.ts";
8+
99
export type Flag = InstanceOf<typeof flagBlock, "#/root/flags">;
1010

1111
export interface FlagObj<TVariant = unknown> {
@@ -76,26 +76,14 @@ const flagBlock: Block<BlockModule<FlagFunc>> = {
7676
},
7777
};
7878
if (isMultivariate(flag)) {
79-
let match: Variant<unknown> | null = null;
80-
81-
for (const variant of flag.variants || []) {
82-
if (typeof variant?.rule !== "function") {
83-
continue;
84-
}
85-
let ruleResult = variant?.rule(ctx);
86-
// the rule can sometimes be a promise and we need to await it to check if it's truthy or not
87-
if (isAwaitable(ruleResult)) {
88-
ruleResult = await ruleResult;
89-
}
90-
if (ruleResult) {
91-
match = variant;
92-
break;
93-
}
94-
}
79+
const variants = flag.variants || [];
9580

96-
if (!match) {
97-
match = flag.variants[flag.variants.length - 1];
98-
}
81+
const results = await Promise.all(
82+
variants.map((variant) =>
83+
typeof variant?.rule === "function" ? variant.rule(ctx) : false
84+
),
85+
);
86+
const match = variants.find((_, index) => results[index]) || variants[0];
9987

10088
return isDeferred(match?.value) ? match?.value() : match?.value;
10189
}

0 commit comments

Comments
 (0)