Skip to content

Commit 39abdbc

Browse files
Use ConvertibleToJSValue for return value type
1 parent 6561e15 commit 39abdbc

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -532,22 +532,22 @@ var expectations: [Expectation] = []
532532

533533
try test("Promise") {
534534

535-
let p1 = JSPromise.resolve(.null)
535+
let p1 = JSPromise.resolve(JSValue.null)
536536
let exp1 = Expectation(label: "Promise.then testcase", expectedFulfillmentCount: 4)
537-
p1.then { (value) -> JSValue in
537+
p1.then { value in
538538
try! expectEqual(value, .null)
539539
exp1.fulfill()
540-
return .number(1.0)
540+
return JSValue.number(1.0)
541541
}
542-
.then { value -> JSValue in
542+
.then { value in
543543
try! expectEqual(value, .number(1.0))
544544
exp1.fulfill()
545-
return JSPromise.resolve(.boolean(true)).jsValue()
545+
return JSPromise.resolve(JSValue.boolean(true))
546546
}
547-
.then { value -> JSValue in
547+
.then { value in
548548
try! expectEqual(value, .boolean(true))
549549
exp1.fulfill()
550-
return .undefined
550+
return JSValue.undefined
551551
}
552552
.catch { _ -> JSValue in
553553
fatalError("Not fired due to no throw")
@@ -559,20 +559,20 @@ try test("Promise") {
559559
p2.then { _ -> JSValue in
560560
fatalError("Not fired due to no success")
561561
}
562-
.catch { reason -> JSValue in
562+
.catch { reason in
563563
try! expectEqual(reason, .boolean(false))
564564
exp2.fulfill()
565-
return .boolean(true)
565+
return JSValue.boolean(true)
566566
}
567-
.then { value -> JSValue in
567+
.then { value in
568568
try! expectEqual(value, .boolean(true))
569569
exp2.fulfill()
570-
return JSPromise.reject(JSValue.number(2.0)).jsValue()
570+
return JSPromise.reject(JSValue.number(2.0))
571571
}
572-
.catch { reason -> JSValue in
572+
.catch { reason in
573573
try! expectEqual(reason, .number(2.0))
574574
exp2.fulfill()
575-
return .undefined
575+
return JSValue.undefined
576576
}
577577
.finally { exp2.fulfill() }
578578

@@ -592,14 +592,14 @@ try test("Promise") {
592592
// verify that at least `timeoutMilliseconds` passed since the timer started
593593
try! expectEqual(start + timeoutMilliseconds <= JSDate().valueOf(), true)
594594
exp3.fulfill()
595-
return .undefined
595+
return JSValue.undefined
596596
}
597597

598598
let exp4 = Expectation(label: "Promise lifetime")
599599
// Ensure that users don't need to manage JSPromise lifetime
600-
JSPromise.resolve(.boolean(true)).then { _ -> JSValue in
600+
JSPromise.resolve(JSValue.boolean(true)).then { _ in
601601
exp4.fulfill()
602-
return .undefined
602+
return JSValue.undefined
603603
}
604604
expectations += [exp1, exp2, exp3, exp4]
605605
}

Sources/JavaScriptKit/BasicObjects/JSPromise.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,44 +66,44 @@ public final class JSPromise: JSBridgedClass {
6666
self.init(unsafelyWrapping: Self.constructor.new(closure))
6767
}
6868

69-
public static func resolve(_ value: JSValue) -> JSPromise {
69+
public static func resolve(_ value: ConvertibleToJSValue) -> JSPromise {
7070
self.init(unsafelyWrapping: Self.constructor.resolve!(value).object!)
7171
}
7272

73-
public static func reject(_ reason: JSValue) -> JSPromise {
73+
public static func reject(_ reason: ConvertibleToJSValue) -> JSPromise {
7474
self.init(unsafelyWrapping: Self.constructor.reject!(reason).object!)
7575
}
7676

7777
/** Schedules the `success` closure to be invoked on sucessful completion of `self`.
7878
*/
7979
@discardableResult
80-
public func then(success: @escaping (JSValue) -> JSValue) -> JSPromise {
80+
public func then(success: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise {
8181
let closure = JSOneshotClosure {
82-
return success($0[0])
82+
return success($0[0]).jsValue()
8383
}
8484
return JSPromise(unsafelyWrapping: jsObject.then!(closure).object!)
8585
}
8686

8787
/** Schedules the `success` closure to be invoked on sucessful completion of `self`.
8888
*/
8989
@discardableResult
90-
public func then(success: @escaping (JSValue) -> JSValue,
91-
failure: @escaping (JSValue) -> JSValue) -> JSPromise {
90+
public func then(success: @escaping (JSValue) -> ConvertibleToJSValue,
91+
failure: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise {
9292
let successClosure = JSOneshotClosure {
93-
return success($0[0])
93+
return success($0[0]).jsValue()
9494
}
9595
let failureClosure = JSOneshotClosure {
96-
return failure($0[0])
96+
return failure($0[0]).jsValue()
9797
}
9898
return JSPromise(unsafelyWrapping: jsObject.then!(successClosure, failureClosure).object!)
9999
}
100100

101101
/** Schedules the `failure` closure to be invoked on rejected completion of `self`.
102102
*/
103103
@discardableResult
104-
public func `catch`(failure: @escaping (JSValue) -> JSValue) -> JSPromise {
104+
public func `catch`(failure: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise {
105105
let closure = JSOneshotClosure {
106-
return failure($0[0])
106+
return failure($0[0]).jsValue()
107107
}
108108
return .init(unsafelyWrapping: jsObject.catch!(closure).object!)
109109
}

0 commit comments

Comments
 (0)