Handling results

How to read the result of a call — immediately, from an event, or as a returned value.

The three ways a call returns

How you get a result depends on the call. There are three patterns.

  1. Fire-and-forget. UI and navigation calls (bdk.navigation.navigate, bdk.ui.showBanner) just do something. await confirms the call went out; there's nothing else to read.
  2. Result on an event. Legacy interactive calls (camera, location, biometrics, purchases) finish later. Subscribe with bdk.on(...) to get the data.
  3. Returned value. Browser envelope helpers return the native outcome through their promise. Server helpers (@thebdk/native/server/*) run on your backend and return typed values.

Legacy commands return NativeCommandResult; use !result.triggered and subscribe before calls whose data arrives on events. A browser tab returns pending, while a runtime without window returns skipped. Envelope helpers return { ok: true, ... } or { ok: false, code, message }; use isBdkError(result) or !result.ok, not triggered. New iap.* and health.* helpers can also return a bare command result: check "ok" in result first. Read each helper’s result type; do not apply one check to all methods.

Browser command options are typed — pass the option names each method documents (e.g. { id, type } for purchases).

import { createBdkNative } from "@thebdk/native/browser";

const bdk = createBdkNative();

// 1. Dispatch-and-forget — result is just the dispatch ack, no event follows.
await bdk.navigation.navigate({ url: new URL("/profile", window.location.origin).href });

// 2. Dispatch + result event — start the interaction here...
bdk.on("photoCaptured", (photo) => console.log(photo.fileUrl));
await bdk.media.capturePhoto();

// 3. Resolved promise with the real value — server only.
// import { verifyIosReceipt } from "@thebdk/native/server/iap";
// const result = await verifyIosReceipt({ receipt });

Fire-and-forget calls

Use these when the app just does something visible — navigate, show a toast, vibrate. await resolves to a NativeCommandResult confirming the call went out. There's no event to listen for.

Choose the native or web UI during startup. Do not wait for a native call merely to discover that the page is on the web. Keep event listeners and result handling specific to the helper you call.

const result = await bdk.navigation.navigate({ url: new URL("/checkout", window.location.origin).href });
// { command: "navigate", queued: true, triggered: true, skipped: false }
// Nothing else arrives — navigation has no result event.

Get a result from an event

Use this when the user has to act — take a photo, pick a date, approve Face ID, confirm a purchase. Subscribe to the event before you make the call; the data arrives on the listener, not on the await.

Event names don't always match the method:

  • bdk.media.capturePhoto() emits photoCaptured.
  • bdk.media.pickPhoto() emits photoSelected.
  • bdk.location.getCurrentPosition() emits location.
  • bdk.ui.pickDateTime() emits datePicked.
  • bdk.auth.authenticateBiometrics() emits biometricResult.
  • bdk.iap.purchaseIos() / purchaseAndroid() emit purchaseSuccess or purchaseFailed.

capturePhoto emits photoCaptured, not photoSelected (which belongs to pickPhoto). Listening on the wrong one is the top reason a result "never arrives".

bdk.on(event, listener) returns an unsubscribe function — call it to stop listening.

// Subscribe first — the result arrives later, on the event.
const off = bdk.on("photoCaptured", (photo) => {
  console.log(photo.fileUrl, photo.dataUri, photo.contentType);
  off(); // unsubscribe when you're done
});

// Dispatch. This resolves on dispatch, NOT when the photo is taken.
await bdk.media.capturePhoto();

Server calls that return a value

Use these on your backend for receipt verification, link creation, and push notifications. They're fully typed and await returns the real result — no event needed.

For example, verifyIosReceipt returns an IosReceiptValidationResult with isValid, resultData, errorData, and raw.

Never import @thebdk/native/server/* into a browser bundle — those modules use credentials and Node APIs. The browser-safe entry points are the package root and @thebdk/native/browser.

import { verifyIosReceipt } from "@thebdk/native/server/iap";

// await gives the real result — no event, no NativeCommandResult.
const result = await verifyIosReceipt({ receipt });
if (result.isValid) {
  grantEntitlement(result.resultData);
}

The NativeCommandResult shape

Legacy commands return NativeCommandResult; use !result.triggered and subscribe before calls whose data arrives on events. A browser tab returns pending, while a runtime without window returns skipped. Envelope helpers return { ok: true, ... } or { ok: false, code, message }; use isBdkError(result) or !result.ok, not triggered. New iap.* and health.* helpers can also return a bare command result: check "ok" in result first. Read each helper’s result type; do not apply one check to all methods.

The common case is { triggered: true }. Outside the app triggered is false — pending in a browser tab, skipped with no window — so branch on !triggered to degrade gracefully.

A few commands are platform-specific (e.g. bdk.iap.purchaseIos is iOS only). On a device that can't run one, the call rejects with a BdkError — wrap it in try/catch, or check bdk.isNative() after await bdk.ready(...) has resolved (it reads false until device info arrives).

interface NativeCommandResult {
  command: string;    // the native command name that was dispatched
  queued: boolean;    // true once the command is scheduled
  triggered: boolean; // was it actually handed to the app?
  skipped: boolean;   // was it dropped without triggering?
  pending?: boolean;  // true while waiting outside the app
  reason?: string;    // why it was skipped / pending, when applicable
}

Why await isn't the result

Await to confirm dispatch, subscribe to receive data.

// ❌ capturePhoto resolves a NativeCommandResult, not a photo.
const result = await bdk.media.capturePhoto();
console.log(result.fileUrl); // undefined — wrong model