Skip to content

Instantly share code, notes, and snippets.

@sjordine
Created November 9, 2020 13:57
Show Gist options
  • Select an option

  • Save sjordine/ad18b3594f9cb72a15dd1a100fcb86d1 to your computer and use it in GitHub Desktop.

Select an option

Save sjordine/ad18b3594f9cb72a15dd1a100fcb86d1 to your computer and use it in GitHub Desktop.
if let context = JSContext() {
let asynchronousCall: @convention(block) (JSValue) -> Void = { (callback) in
loadCatFact { (catFact) in
callback.call(withArguments: [catFact])
}
}
let printText:@convention(block) (String) -> Void = { (text) in
print("\(text)")
}
context.setObject(asynchronousCall,
forKeyedSubscript: "loadFact" as NSString)
context.setObject(printText,
forKeyedSubscript: "print" as NSString)
let result = context.evaluateScript("""
print("before");
loadFact(result => {
print("Raw data: " + result);
fact = JSON.parse(result);
print("Fact: " + fact.fact);
});
print("after");
""")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment