-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.carb
35 lines (28 loc) · 1 KB
/
async.carb
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
35
/*
* Async is used to provide carbon code with a generic way to use native javascript functions.
* Carbide may implement a proper event loop in the future and as such will rely heavily on async.
*/
class Testing {
fixed async int wait(int seconds) {
native javascript {
setTimeout(() => {@return(seconds)}, seconds * 1000);
} else { // The native else branch is not yet supported in Carbonite
return seconds;
}
}
}
class App {
int main(<string>array args) {
let result = Testing.wait(4);
// Note: Async calls are pre fetched before being evaluated in expressions.
// As a result the following two waits will be called before the if statement.
// This goes againts normal logical rules and should be resolved in the future.
if (this.wait(2) != 2 and this.wait(1) == 1)
Console.log("True");
// Example of async logic flow:
let outputOfFirstWait = this.wait(2);
let outputOfSecondWait = this.wait(1);
if (outputOfFirstWait != 2 and outputOfSecondWait == 1)
Console.log("True");
}
}