Skip to content

Commit e002eb8

Browse files
committed
Updated JSPromise
1 parent ea55aa5 commit e002eb8

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

Sources/JavaScriptKit/JS Types/JSPromise.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public final class JSPromise<Success: JSType>: JSType {
9191
*/
9292
@discardableResult
9393
internal func _then(onFulfilled: @escaping (Success) -> (JSValue),
94-
onRejected: @escaping (JSError) -> () = defaultRejected) -> JSValue {
94+
onRejected: @escaping (JSError) -> ()) -> JSValue {
9595

9696
guard let function = jsObject.then.function
9797
else { fatalError("Invalid function \(#function)") }
@@ -117,6 +117,24 @@ public final class JSPromise<Success: JSType>: JSType {
117117
return function.apply(this: jsObject, argumentList: [success.jsValue(), errorFunction.jsValue()])
118118
}
119119

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+
120138
/**
121139
The `then()` method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.
122140

@@ -138,7 +156,7 @@ public final class JSPromise<Success: JSType>: JSType {
138156
onRejected: @escaping (JSError) -> ()) -> JSPromise<Success> {
139157
let result = _then(onFulfilled: {
140158
onFulfilled($0)
141-
return .undefined
159+
return .null
142160
}, onRejected: onRejected)
143161
guard let promise = result.object.flatMap({ JSPromise<Success>($0) })
144162
else { fatalError("Invalid object \(result)") }
@@ -193,7 +211,7 @@ public final class JSPromise<Success: JSType>: JSType {
193211
public func then(onFulfilled: @escaping (Success) -> ()) -> JSPromise<Success> {
194212
let result = _then(onFulfilled: {
195213
onFulfilled($0)
196-
return .undefined
214+
return .null
197215
})
198216
guard let promise = result.object.flatMap({ JSPromise<Success>($0) })
199217
else { fatalError("Invalid object \(result)") }
@@ -250,8 +268,6 @@ public final class JSPromise<Success: JSType>: JSType {
250268

251269
private let JSPromiseClassObject = JSObjectRef.global.Promise.function!
252270

253-
internal let defaultRejected: (JSError) -> () = { JSConsole.error("Uncaught promise error ", $0) }
254-
255271
// MARK: - Supporting Types
256272

257273
public extension JSPromise {

0 commit comments

Comments
 (0)