forked from uNetworking/uWebSockets.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncFunction.js
43 lines (37 loc) · 1.01 KB
/
AsyncFunction.js
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
/* SSL/non-SSL example with async/await functions */
function delay(t, val) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve(val);
}, t);
});
}
async function someAsyncTask() {
return delay(500, 'Hey wait for me!');
}
const uWS = require('../dist/uws.js');
const port = 9001;
const app = uWS./*SSL*/App({
key_file_name: 'misc/key.pem',
cert_file_name: 'misc/cert.pem',
passphrase: '1234'
}).get('/*', async (res) => {
/* Can't return or yield from here without responding or attaching an abort handler */
res.onAborted(() => {
res.aborted = true;
});
/* Awaiting will yield and effectively return to C++, so you need to have called onAborted */
let r = await someAsyncTask();
/* If we were aborted, you cannot respond */
if (!res.aborted) {
res.cork(() => {
res.end(r);
});
}
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
} else {
console.log('Failed to listen to port ' + port);
}
});