Add async/await example

This commit is contained in:
Alex Hultman 2019-02-08 21:42:31 +01:00
parent 80f10df8de
commit 54ebed1db0

33
examples/AsyncFunction.js Normal file
View File

@ -0,0 +1,33 @@
/* 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);
}
});