uWebSockets.js/examples/AsyncFunction.js

34 lines
851 B
JavaScript
Raw Normal View History

2019-02-08 20:42:31 +00:00
/* SSL/non-SSL example with async/await functions */
async function someAsyncTask() {
return '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.end(r);
}
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
} else {
console.log('Failed to listen to port ' + port);
}
});