Files
fil/docs/snippets/wasm/api/error_handling_extract.md
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

1.3 KiB

import init, { extractBytes } from "kreuzberg-wasm";

await init();

// Note: WASM has no native batch API; use Promise.all with per-item error handling
const files = document.getElementById("files") as HTMLInputElement;
const fileList = files.files || [];

// Extract multiple files concurrently (simulated batch)
const extractionPromises = Array.from(fileList).map(async (file) => {
  try {
    const bytes = new Uint8Array(await file.arrayBuffer());
    const result = await extractBytes(bytes, file.type || "application/octet-stream", undefined);
    return { file: file.name, success: true, result };
  } catch (err) {
    return {
      file: file.name,
      success: false,
      error: err instanceof Error ? err.message : String(err),
    };
  }
});

const results = await Promise.all(extractionPromises);

// Process results with per-item error handling
results.forEach((item) => {
  if (item.success) {
    console.log(`✓ ${item.file}: ${item.result.content.length} characters`);
  } else {
    console.error(`✗ ${item.file}: ${item.error}`);
  }
});

// Summary
const succeeded = results.filter((r) => r.success).length;
const failed = results.filter((r) => !r.success).length;
console.log(`Extracted ${succeeded}/${results.length} files (${failed} errors)`);