Ben 11 meses atrás
pai
commit
f9453c8ad9
1 arquivos alterados com 6 adições e 23 exclusões
  1. 6 23
      js/main.swift

+ 6 - 23
js/main.swift

@@ -95,30 +95,13 @@ func testDetail(url: String, ctx: JSContext) -> Void {
 }
 
 func testSearch(keyword: String, ctx: JSContext) -> Void {
-    if let searchFunction = ctx.objectForKeyedSubscript("search") {
-        print(searchFunction)
-        let result = searchFunction.call(withArguments: [keyword, nil])
-        print(result?.toString())
-        let resolveHandler: @convention(block) (JSValue?) -> Void = { resolvedValue in
-            if let value = resolvedValue {
-                print("搜索成功: ", value)
-            }
-        }
-        let rejectHandler: @convention(block) (JSValue?) -> Void = { rejectedValue in
-            if let value = rejectedValue {
-                print("搜索失败:", value)
-            }
-        }
-        if let thenFunction = result?.objectForKeyedSubscript("then") {
-            print("Then:", thenFunction.toString())
-            // 调用 then 函数,并传入回调函数
-            thenFunction.call(withArguments: [unsafeBitCast(resolveHandler, to: AnyObject.self)])
-        }
-        if let catchFunction = result?.objectForKeyedSubscript("catch") {
-            print("Catch:", catchFunction.toString())
-            // 调用 catch 函数,并传入回调函数
-            catchFunction.call(withArguments: [unsafeBitCast(rejectHandler, to: AnyObject.self)])
+    if let search = ctx.objectForKeyedSubscript("search") {
+        let promise = search.call(withArguments: [keyword])
+        let thenCallback: @convention(block) (String) -> Void = { result in
+            print("搜索结果: ", result)
         }
+        let then = promise?.objectForKeyedSubscript("then")
+        then?.call(withArguments: [thenCallback])
     }
 }