@@ -91,7 +91,7 @@ public final class JSPromise<Success: JSType>: JSType {
91
91
*/
92
92
@discardableResult
93
93
internal func _then( onFulfilled: @escaping ( Success ) -> ( JSValue ) ,
94
- onRejected: @escaping ( JSError ) -> ( ) = defaultRejected ) -> JSValue {
94
+ onRejected: @escaping ( JSError ) -> ( ) ) -> JSValue {
95
95
96
96
guard let function = jsObject. then. function
97
97
else { fatalError ( " Invalid function \( #function) " ) }
@@ -117,6 +117,24 @@ public final class JSPromise<Success: JSType>: JSType {
117
117
return function. apply ( this: jsObject, argumentList: [ success. jsValue ( ) , errorFunction. jsValue ( ) ] )
118
118
}
119
119
120
+ @discardableResult
121
+ internal func _then( onFulfilled: @escaping ( Success ) -> ( JSValue ) ) -> JSValue {
122
+
123
+ guard let function = jsObject. then. function
124
+ else { fatalError ( " Invalid function \( #function) " ) }
125
+
126
+ let success = JSFunctionRef . from { ( arguments) in
127
+ if let value = arguments. first. flatMap ( { Success . construct ( from: $0) } ) {
128
+ return onFulfilled ( value)
129
+ } else {
130
+ JSConsole . error ( " Unable to load success type \( String ( reflecting: Success . self) ) from " , arguments. first ?? " nil " )
131
+ return . undefined
132
+ }
133
+ }
134
+
135
+ return function. apply ( this: jsObject, argumentList: [ success. jsValue ( ) ] )
136
+ }
137
+
120
138
/**
121
139
The `then()` method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.
122
140
@@ -138,7 +156,7 @@ public final class JSPromise<Success: JSType>: JSType {
138
156
onRejected: @escaping ( JSError ) -> ( ) ) -> JSPromise < Success > {
139
157
let result = _then ( onFulfilled: {
140
158
onFulfilled ( $0)
141
- return . undefined
159
+ return . null
142
160
} , onRejected: onRejected)
143
161
guard let promise = result. object. flatMap ( { JSPromise < Success > ( $0) } )
144
162
else { fatalError ( " Invalid object \( result) " ) }
@@ -193,7 +211,7 @@ public final class JSPromise<Success: JSType>: JSType {
193
211
public func then( onFulfilled: @escaping ( Success ) -> ( ) ) -> JSPromise < Success > {
194
212
let result = _then ( onFulfilled: {
195
213
onFulfilled ( $0)
196
- return . undefined
214
+ return . null
197
215
} )
198
216
guard let promise = result. object. flatMap ( { JSPromise < Success > ( $0) } )
199
217
else { fatalError ( " Invalid object \( result) " ) }
@@ -250,8 +268,6 @@ public final class JSPromise<Success: JSType>: JSType {
250
268
251
269
private let JSPromiseClassObject = JSObjectRef . global. Promise. function!
252
270
253
- internal let defaultRejected : ( JSError ) -> ( ) = { JSConsole . error ( " Uncaught promise error " , $0) }
254
-
255
271
// MARK: - Supporting Types
256
272
257
273
public extension JSPromise {
0 commit comments