forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPauseExecution.swift
More file actions
34 lines (30 loc) · 1.43 KB
/
Copy pathPauseExecution.swift
File metadata and controls
34 lines (30 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import _CJavaScriptKit
/// Unwind Wasm module execution stack and rewind it after specified milliseconds,
/// allowing JavaScript events to continue to be processed.
/// **Important**: Wasm module must be [asyncified](https://emscripten.org/docs/porting/asyncify.html),
/// otherwise JavaScriptKit's runtime will throw an exception.
public func pauseExecution(milliseconds: Int32) {
_sleep(milliseconds)
}
extension JSPromise {
/// Unwind Wasm module execution stack and rewind it after promise resolves,
/// allowing JavaScript events to continue to be processed in the meantime.
/// - Parameters:
/// - timeout: If provided, method will return a failure if promise cannot resolve
/// before timeout is reached.
///
/// **Important**: Wasm module must be [asyncified](https://emscripten.org/docs/porting/asyncify.html),
/// otherwise JavaScriptKit's runtime will throw an exception.
public func syncAwait() -> Result<JSValue, JSValue> {
var kindAndFlags = JavaScriptValueKindAndFlags()
var payload1 = JavaScriptPayload1()
var payload2 = JavaScriptPayload2()
_syncAwait(jsObject.id, &kindAndFlags, &payload1, &payload2)
let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2).jsValue()
if kindAndFlags.isException {
return .failure(result)
} else {
return .success(result)
}
}
}