Skip to content

BridgeJS: support imports of JS Promise as async Swift#707

Merged
kateinoigakukun merged 21 commits intomainfrom
maxd/async-bridgejs
Mar 27, 2026
Merged

BridgeJS: support imports of JS Promise as async Swift#707
kateinoigakukun merged 21 commits intomainfrom
maxd/async-bridgejs

Conversation

@MaxDesiatov
Copy link
Member

@MaxDesiatov MaxDesiatov commented Mar 23, 2026

Supersedes #486.

Provides a motivating example with fetchWeatherData() test. The full end-to-end flow works:

  1. TypeScript interface WeatherData { temperature: number; description: string; humidity: number; };
  2. Function fetchWeatherData(city: string): Promise<WeatherData>;
  3. JS returns Promise.resolve({ temperature: 15.5, description: "Cloudy", humidity: 80 });
  4. Swift awaits and gets a typed WeatherData struct with .temperature, .description, .humidity properties;
  5. No Foundation, no Codable, no manual JSValue unwrapping for this and analogous functions backed by fetch(...).json().

Compatible with Embedded Swift.

Additionally, @JSFunction now supports async effects.

@MaxDesiatov MaxDesiatov requested review from kateinoigakukun and krodak and removed request for kateinoigakukun March 23, 2026 23:15
@MaxDesiatov MaxDesiatov marked this pull request as ready for review March 23, 2026 23:16
@MaxDesiatov MaxDesiatov added enhancement New feature or request bridgejs labels Mar 23, 2026
@MaxDesiatov MaxDesiatov changed the title BridgeJS: support imports of Promise JS as async Swift BridgeJS: support imports of JS Promise as async Swift Mar 23, 2026
Copy link
Member

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a different approach using JSTypedClosure to avoid the boxing

@MaxDesiatov MaxDesiatov marked this pull request as draft March 24, 2026 15:00
@MaxDesiatov MaxDesiatov removed the request for review from krodak March 24, 2026 15:00
@MaxDesiatov MaxDesiatov marked this pull request as ready for review March 24, 2026 16:51
import Testing
import JavaScriptKit

@Suite struct AsyncImportTests {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate? Which specific pattern are you referring to? I suppose that's not the use of XCTest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, I'd like to avoid adding more and more imports in prelude.mjs and want to split into separate file like this: https://github.com/swiftwasm/JavaScriptKit/blob/main/Tests/BridgeJSRuntimeTests/JavaScript/ArraySupportTests.mjs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, also addressed

@kateinoigakukun kateinoigakukun merged commit 7e242b0 into main Mar 27, 2026
12 checks passed
@kateinoigakukun kateinoigakukun deleted the maxd/async-bridgejs branch March 27, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bridgejs enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants