-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample.smash
More file actions
224 lines (181 loc) 路 5.32 KB
/
Copy pathexample.smash
File metadata and controls
224 lines (181 loc) 路 5.32 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
let x = [1, 2, 3];
const y = [true, false, false, true];
};
print("age:", user.age);
for (let i of x) {
print(i);
}
// Iterate over object properties with for...in loop
for (let val in user) {
print(val, user[val]);
}
for (let x of y) {
if (x) {
print("true");
continue;
} else {
print("false");
}
}
// Sleep function using Promise with setTimeout
async fn sleep(ms) {
print("Sleeping...");
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
// Test async function
async fn test() {
print("Starting test");
const result = await sleep(1000);
print("Finished sleeping");
return result;
}
// Call the async function
print("Before await");
const result = await test();
print("After await");
print("Result:");
print(result);
let message = "Hello, SmashLang!";
let year = 2025;
let pi = 3.14;
let active = true;
let active2 = false;
if (active) {
print(pi);
}
if (!active2) {
print("not active", pi);
}
// Using a string-based approach for regex patterns
let pattern = "smash.*";
print(message);
// Simple Promise test
print("Promise test:");
// Create a function that returns a promise
fn createPromise() {
return new Promise((resolve, reject) => {
// Resolve after a delay
setTimeout(() => {
resolve("Promise resolved!");
}, 1000);
});
}
// Create a function to handle the Promise result
fn handlePromise(result) {
print("Promise result:", result);
return "Processed: " + result;
}
// Create a function to handle errors
fn handleError(error) {
print("Promise error:", error);
}
// Use the Promise
const promiseResult = createPromise();
// Add handlers manually
promiseResult.then(handlePromise);
promiseResult.onCatch(handleError);
// Example of try/catch/finally blocks
print("Try/Catch/Finally example:");
try {
print("Inside try block");
// Throw an error
throw new Error("Something went wrong");
print("This will not be executed");
} catch (error) {
print("Inside catch block");
print("Error message: " + error);
} finally {
print("Inside finally block - this always executes");
}
// Example of fetch with Promise chaining
print("Fetch example with Promise chaining:");
// Using variable assignment for method chaining (recommended approach)
const fetchTodoResult = fetch("https://jsonplaceholder.typicode.com/todos/1");
const jsonTodoResult = fetchTodoResult.then((response) => {
print("Todo response status: " + response.status);
return response.json();
});
const processedTodoResult = jsonTodoResult.then((data) => {
print("Todo title: " + data.title);
print("Todo completed: " + data.completed);
return data;
});
processedTodoResult.onCatch((error) => {
print("Fetch todo error: " + error);
});
// Example of fetching user data
print("Fetching user data:");
const fetchUserResult = fetch("https://jsonplaceholder.typicode.com/users/1");
const jsonUserResult = fetchUserResult.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok: " + response.status);
}
return response.json();
});
const processedUserResult = jsonUserResult.then((userData) => {
print("User name: " + userData.name);
print("User email: " + userData.email);
print("User company: " + userData.company.name);
return userData;
});
processedUserResult.onCatch((error) => {
print("Fetch user error: " + error);
});
// Example of fetching posts with async/await
print("Fetching posts with async/await:");
async fn fetchPosts() {
try {
const response = await fetch("https://jsonplaceholder.typicode.com/posts?userId=1");
if (!response.ok) {
throw new Error("Failed to fetch posts: " + response.status);
}
const posts = await response.json();
print("Number of posts: " + posts.length);
// Display the first post
if (posts.length > 0) {
const firstPost = posts[0];
print("First post title: " + firstPost.title);
print("First post body: " + firstPost.body);
}
return posts;
} catch (error) {
print("Error fetching posts: " + error);
return [];
}
}
// Call the async function to fetch posts
const postsResult = await fetchPosts();
print(postsResult);
// Simple async/await test
print("Async/await test:");
// Helper function that returns a promise
fn createDelayPromise(ms, value) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(value);
}, ms);
});
}
// Define an async function
async fn processAsync() {
try {
print("Starting async operation...");
// Use await with our delay function
const result1 = await createDelayPromise(500, "First result");
print("Got first result:", result1);
// Chain another async operation
const result2 = await createDelayPromise(500, "Second result: " + result1);
print("Got second result:", result2);
return "Completed: " + result2;
} catch (error) {
print("Async error:", error);
return null;
}
}
// Call the async function
print("Before calling async function");
const asyncResult = await processAsync();
print("After async function");
print("Final result:", asyncResult);