PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
70 stars 36 forks source link

Asynchronous calls of ´isFeatureEnabled´ fail to resolve #80

Closed babasbot closed 1 year ago

babasbot commented 1 year ago

Bug description

Asynchronous calls of isFeatureEnabled fail to resolve when no personal API key is provided.

Tested on:

How to reproduce

import { PostHog } from "posthog-node";

const { POSTHOG_API_KEY } = process.env;

if (!POSTHOG_API_KEY) {
    throw Error("PostHog API key not provided.");
}

const posthog = new PostHog(POSTHOG_API_KEY);

async function main() {
    const distinctId = "1337";
    const key = "AWESOME_FEATURE";

    const featureFlags = await Promise.all([
        posthog.isFeatureEnabled(key, distinctId),
        posthog.isFeatureEnabled(key, distinctId),
        posthog.isFeatureEnabled(key, distinctId),
        posthog.isFeatureEnabled(key, distinctId),
        posthog.isFeatureEnabled(key, distinctId),
        posthog.isFeatureEnabled(key, distinctId),
        posthog.isFeatureEnabled(key, distinctId),
        posthog.isFeatureEnabled(key, distinctId),
    ]);

    console.log(featureFlags);
}

main()
    .then(() => {
        process.exit(0);
    })
    .catch((err) => {
        console.error(err);
        process.exit(-1);
    });

The expected result would be:

[
  true, true,
  true, true,
  true, true,
  true, true
]

But instead, we got:

[
  undefined, true,
  undefined, undefined,
  undefined, undefined,
  undefined, undefined
]

Related sub-libraries

Additional context

for (let i = 0; i < 8; i++) {
    const ff = await posthog.isFeatureEnabled(key, distinctId);
    console.log(ff);
}
// -> false
// -> false
// -> false
// -> false
// -> false
// -> false
// -> false
// -> false
neilkakkar commented 1 year ago

Hey @babasbot , mind checking if this holds true on v2.5.0 as well please? It was released today, with a pretty big change to how the internals here work.

Thanks!

babasbot commented 1 year ago

It is working as expected on 2.5.0; thank you! 🖖