-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-intro-to-futures.cfc
115 lines (104 loc) · 2.89 KB
/
01-intro-to-futures.cfc
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* =================================================================================
* THIS IS THE SOLUTION FILE. TO WORK ON THE EXERCISES GO TO THE `exercises` FOLDER.
* =================================================================================
*
* This exercise goes over:
* + creating new Futures
* + running Futures
* + waiting for Futures to complete
* + Assigning timeouts and default values to Futures
*
* You can run this exercise from the root in CommandBox by running:
* `task run exercises/01-intro-to-futures.cfc`
*
* You can use the `print` helper provided by CommandBox to log messages.
* (Use `print.line( "message" ).toConsole()` if you're not familiar with it.)
* Make sure to end with `.toConsole()` so your message is flushed
* to the console the moment it is printed.
*/
component extends="../BaseTask" {
/**
* Create a future that prints out a greeting immediately.
*/
function partOne(){
asyncManager.newFuture( () => {
print.line( "Hi!" ).toConsole();
} );
}
/**
* Create a future that prints out a greeting after 2 seconds.
*/
function partTwo(){
asyncManager.newFuture( () => {
sleep( 2000 );
print.line( "Hi again!" ).toConsole();
} );
}
/**
* Create a future that _returns_ a message to print out after 5 seconds.
* Print out that message on the main thread.
*/
function partThree(){
var future = asyncManager.newFuture( () => {
sleep( 5000 );
return "Please wait for my greeting.";
} );
print.line( future.get() ).toConsole();
}
/**
* Create a future that _returns_ a message to print out after 5 seconds.
* Wait for a 3 second timeout before returning a default value.
* Print out that message on the main thread.
*/
function partFour(){
var future = asyncManager.newFuture( () => {
sleep( 5000 );
return "Please wait for my greeting.";
} );
print
.line(
future.get(
timeout = 3000,
defaultValue = "Nevermind....Goodbye."
)
)
.toConsole();
}
/**
* Create a future that _returns_ a message to print out after 5 seconds.
* Wait for a 3 second timeout. Do not return a default value.
* See what happens on the main thread
* - when you do not wait for the future to complete?
* - when you wait for the future to complete?
*/
function partFive(){
var future = asyncManager.newFuture( () => {
sleep( 5000 );
return "Please wait for my greeting.";
} );
try {
future.get( timeout = 3000 );
} catch ( any e ) {
print.redLine( "Whoops! We ran in to a timeout here." ).toConsole();
}
}
function run(){
print.blueLine( "Running all `01-intro-to-futures` exercises" ).toConsole();
print
.yellowLine( "Press Ctrl-C to exit" )
.line()
.toConsole();
partOne();
partTwo();
partThree();
partFour();
partFive();
while ( true ) {
if ( !isNull( checkInterrupted() ) ) {
return;
}
sleep( 100 );
}
}
}