@@ -2897,9 +2897,8 @@ extension BridgeJSLink {
28972897
28982898 /// Renders an async import function with resolve/reject closure refs.
28992899 ///
2900- /// The generated function takes `resolveRef` and `rejectRef` as the first parameters,
2901- /// looks up the resolve/reject closures from memory, and executes the body which
2902- /// chains `.then(resolve, reject)` on the import's returned Promise.
2900+ /// The generated function takes `resolveRef` and `rejectRef` as the first parameters
2901+ /// and settles them with the result of the import call.
29032902 private func renderAsyncFunction( name: String ? ) -> [ String ] {
29042903 let printer = CodeFragmentPrinter ( )
29052904 let allParams = [ " resolveRef " , " rejectRef " ] + parameterNames
@@ -2908,7 +2907,15 @@ extension BridgeJSLink {
29082907 let s = JSGlueVariableScope . reservedSwift
29092908 printer. write ( " const resolve = \( s) .memory.getObject(resolveRef); " )
29102909 printer. write ( " const reject = \( s) .memory.getObject(rejectRef); " )
2911- printer. write ( contentsOf: body)
2910+ printer. write ( " try { " )
2911+ printer. indent {
2912+ printer. write ( contentsOf: body)
2913+ }
2914+ printer. write ( " } catch (error) { " )
2915+ printer. indent {
2916+ printer. write ( " reject(error); " )
2917+ }
2918+ printer. write ( " } " )
29122919 }
29132920 printer. write ( " } " )
29142921 return printer. lines
@@ -2930,7 +2937,7 @@ extension BridgeJSLink {
29302937
29312938 private func call( callExpr: String ) throws {
29322939 if effects. isAsync {
2933- body. write ( " \( callExpr) .then(resolve, reject); " )
2940+ body. write ( " Promise.resolve( \( callExpr) ).then(resolve, reject); " )
29342941 return
29352942 }
29362943 if let name = returnType. referencedGenericName {
@@ -2997,14 +3004,6 @@ extension BridgeJSLink {
29973004 return try call ( calleeExpr: calleeExpr)
29983005 }
29993006
3000- /// Generates an async method call with resolve/reject closure refs.
3001- func callAsyncMethod( name: String ) {
3002- let objectExpr = " \( JSGlueVariableScope . reservedSwift) .memory.getObject(self) "
3003- let calleeExpr = Self . propertyAccessExpr ( objectExpr: objectExpr, propertyName: name)
3004- let callExpr = " \( calleeExpr) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
3005- body. write ( " \( callExpr) .then(resolve, reject); " )
3006- }
3007-
30083007 func callStaticMethod( on objectExpr: String , name: String ) throws {
30093008 let calleeExpr = Self . propertyAccessExpr ( objectExpr: objectExpr, propertyName: name)
30103009 return try call ( calleeExpr: calleeExpr)
@@ -4235,7 +4234,7 @@ extension BridgeJSLink {
42354234 method: ExportedFunction
42364235 ) throws {
42374236 let thunkBuilder = ImportedThunkBuilder (
4238- effects: Effects ( isAsync : false , isThrows : false ) ,
4237+ effects: method . effects ,
42394238 returnType: method. returnType,
42404239 context: . exportSwift,
42414240 intrinsicRegistry: intrinsicRegistry
0 commit comments