-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Seems async Task
doesn't work
#5376
Comments
I just tried to use The only place where static func main() async {
Task {
print("🔆🔆🔆🔆🔆🔆") // works
}
} But it never works anywhere later. And btw it throws this in the WebInspector console Not sure if I'm missing something. |
One more example import JavaScriptKit
@main
public struct CartonApp {
public private(set) var text = "Hello, World!"
public static func main() async {
print(CartonApp().text)
if let docObj = JSObject.global.document.object, let bodyObj = JSObject.global.document.jsValue.body.object {
var button = JSObject.global.document.createElement.function?.callAsFunction(this: docObj, "button").jsValue
button?.innerText = "Click me".jsValue
JSObject.global.document.jsValue.body.appendChild.function?.callAsFunction(this: bodyObj, button)
if let buttonObj = button?.object {
button?.addEventListener.function?.callAsFunction(this: buttonObj, "click", JSClosure({ args in
print("clicked") // works
Task {
print("clicked inside of Task") // never called
}
return .undefined
}))
}
}
}
}
button?.addEventListener.function?.callAsFunction(this: buttonObj, "click", JSClosure.async({ args in
print("clicked async") // never called
return .undefined
})) |
Thanks for reporting. This code however won't run as you expect, even on macOS:
It's a misunderstanding of the main function's lifetime. Once you start the task, the synchronous context reaches its end and there's nothing telling it to keep alive. |
@yonihemi Thank you for the reply. Could you please help with import JavaScriptKit
@main
public struct CartonApp {
public private(set) var text = "Hello, World!"
public static func main() async {
print(CartonApp().text)
if let docObj = JSObject.global.document.object, let bodyObj = JSObject.global.document.jsValue.body.object {
var button = JSObject.global.document.createElement.function?.callAsFunction(this: docObj, "button").jsValue
button?.innerText = "Click me".jsValue
JSObject.global.document.jsValue.body.appendChild.function?.callAsFunction(this: bodyObj, button)
if let buttonObj = button?.object {
button?.addEventListener.function?.callAsFunction(this: buttonObj, "click", JSClosure.async({ args in
print("clicked async") // never called
return .undefined
}))
}
}
}
} |
I don't see calls to |
That's awesome, that's exactly what I missed!🔥 Thank you very much for pointing me @MaxDesiatov I appreciate it! |
Description
I'm trying to execute async code wrapped into
Task
but it never called.Steps to reproduce
carton init
Expected behavior
async Task works
should be printed into consoleEnvironment
@kateinoigakukun
The text was updated successfully, but these errors were encountered: