I am trying to execute an async function from a javascript SDK in Swift using JSCore. I have been able to accomplish evaluating and viewing the contents of the script but have been unsuccessful in actually calling the async function. Here is my code to verify I have even read the script. Any thoughts on how to accomplish this?
lazy var context: JSContext? = {
let context = JSContext()
guard let
commonJSPath = Bundle.main.path(forResource: "test", ofType: "js") else {
print("Unable to read resource files.")
return nil
}
do {
let common = try String(contentsOfFile: commonJSPath, encoding: String.Encoding.utf8)
_ = context?.evaluateScript(common)
} catch (let error) {
print("Error while processing script file: \(error)")
}
return context
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupJS()
}
func setupJS(){
guard let context = context else {
print("JSContext not found.")
return
}
let dictionary = context.objectForKeyedSubscript("MainObject").toDictionary()
print(dictonary)
//let script = "const {variableName} = FooJS;";
//let result = context.evaluateScript(script)
}
I'd like to do something like async function doSomething(){ return 'hello world!'; }
console.log(await doSomething());