Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • LDClient

Index

Methods

  • allFlagsState(user: LDUser, options?: LDFlagsStateOptions, callback?: (err: Error, res: LDFlagsState) => void): Promise<LDFlagsState>
  • Builds an object that encapsulates the state of all feature flags for a given user. This includes the flag values and also metadata that can be used on the front end. This method does not send analytics events back to LaunchDarkly.

    The most common use case for this method is to bootstrap a set of client-side feature flags from a back-end service. Call the toJSON() method of the returned object to convert it to the data structure used by the client-side SDK.

    Parameters

    • user: LDUser

      The end user requesting the feature flags.

    • Optional options: LDFlagsStateOptions

      Optional [[LDFlagsStateOptions]] to determine how the state is computed.

    • Optional callback: (err: Error, res: LDFlagsState) => void

      A Node-style callback to receive the result (as an [[LDFlagsState]]). If omitted, you will receive a Promise instead.

        • (err: Error, res: LDFlagsState): void
        • Parameters

          • err: Error
          • res: LDFlagsState

          Returns void

    Returns Promise<LDFlagsState>

    If you provided a callback, then nothing. Otherwise, a Promise which will be resolved with the result as an [[LDFlagsState]].

  • variation(key: string, user: LDUser, defaultValue: any, callback?: (err: any, res: any) => void): Promise<any>
  • Determines the variation of a feature flag for a user.

    Parameters

    • key: string

      The unique key of the feature flag.

    • user: LDUser

      The end user requesting the flag. The client will generate an analytics event to register this user with LaunchDarkly if the user does not already exist.

    • defaultValue: any

      The default value of the flag, to be used if the value is not available from the KV store.

    • Optional callback: (err: any, res: any) => void

      A Node-style callback to receive the result value. If omitted, you will receive a Promise instead.

        • (err: any, res: any): void
        • Parameters

          • err: any
          • res: any

          Returns void

    Returns Promise<any>

    If you provided a callback, then nothing. Otherwise, a Promise which will be resolved with the result value.

  • variationDetail(key: string, user: LDUser, defaultValue: any, callback?: (err: any, res: LDEvaluationDetail) => void): Promise<LDEvaluationDetail>
  • Determines the variation of a feature flag for a user, along with information about how it was calculated.

    The reason property of the result will also be included in analytics events, if you are capturing detailed event data for this flag.

    For more information, see the SDK reference guide.

    Parameters

    • key: string

      The unique key of the feature flag.

    • user: LDUser

      The end user requesting the flag. The client will generate an analytics event to register this user with LaunchDarkly if the user does not already exist.

    • defaultValue: any

      The default value of the flag, to be used if the value is not available from the KV store.

    • Optional callback: (err: any, res: LDEvaluationDetail) => void

      A Node-style callback to receive the result (as an [[LDEvaluationDetail]]). If omitted, you will receive a Promise instead.

        • (err: any, res: LDEvaluationDetail): void
        • Parameters

          • err: any
          • res: LDEvaluationDetail

          Returns void

    Returns Promise<LDEvaluationDetail>

    If you provided a callback, then nothing. Otherwise, a Promise which will be resolved with the result (as an [[LDEvaluationDetail]]).

  • waitForInitialization(): Promise<LDClient>
  • Returns a Promise that tracks the client's initialization state.

    The Promise will be resolved if the client successfully initializes, or rejected if client initialization has failed unrecoverably (for instance, if it detects that the SDK key is invalid). Keep in mind that unhandled Promise rejections can be fatal in Node, so if you call this method, be sure to attach a rejection handler to it (or, if using async/await, a catch block).

    Note that you can also use event listeners ([[on]]) for the same purpose: the event "ready" indicates success, and "failed" indicates failure.

    There is no built-in timeout for this method. If you want your code to stop waiting on the Promise after some amount of time, you could use Promise.race() or one of the several NPM helper packages that provides a standard mechanism for this. Regardless of whether you continue to wait, the SDK will still retry all connection failures indefinitely unless it gets an unrecoverable error as described above.

    example

    This example shows use of Promise chaining methods for specifying handlers:

      client.waitForInitialization().then(() => {
    // do whatever is appropriate if initialization has succeeded
    }).catch(err => {
    // do whatever is appropriate if initialization has failed
    })
    example

    This example shows use of async/await syntax for specifying handlers:

      try {
    await client.waitForInitialization();
    // do whatever is appropriate if initialization has succeeded
    } catch (err) {
    // do whatever is appropriate if initialization has failed
    }

    Returns Promise<LDClient>

    A Promise that will be resolved if the client initializes successfully, or rejected if it fails. If successful, the result is the same client object.

Generated using TypeDoc