Created
September 27, 2018 09:11
-
-
Save pmlopes/ce9becabcb4f147e0df3168a596acaa3 to your computer and use it in GitHub Desktop.
Async Await with ES4X and GraalVM - https://www.jetdrone.xyz/2018/09/27/Who-needs-coroutines-in-java.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types="@vertx/core/runtime" /> | |
// @ts-check | |
const util = require('util'); | |
import {Router} from '@vertx/web'; | |
import {PgClient, Tuple} from '@reactiverse/reactive-pg-client'; | |
import {PgPoolOptions} from '@reactiverse/reactive-pg-client/options'; | |
const SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1"; | |
const app = Router.router(vertx); | |
let client = PgClient.pool( | |
vertx, | |
new PgPoolOptions() | |
.setUser('benchmarkdbuser') | |
.setPassword('benchmarkdbpass') | |
.setDatabase('hello_world')); | |
app.get('/').handler( | |
// // Step #1 | |
// ctx => { | |
// client.preparedQuery(SELECT_WORLD, Tuple.of(1), res => { | |
// if (res.succeeded()) { | |
// let resultSet = res.result().iterator(); | |
// | |
// if (!resultSet.hasNext()) { | |
// // no result found | |
// ctx.fail(404); | |
// return; | |
// } else { | |
// let row = resultSet.next(); | |
// | |
// ctx.response() | |
// .putHeader("Content-Type", "application/json") | |
// .end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)})); | |
// } | |
// } else { | |
// ctx.fail(res.cause()); | |
// } | |
// }); | |
// } | |
// // Step #2 | |
// ctx => { | |
// // vert.x promise | |
// let postgres = util.promisify(client); | |
// | |
// postgres | |
// .preparedQuery(SELECT_WORLD, Tuple.of(1)) | |
// .then(rs => { | |
// let resultSet = rs.iterator(); | |
// | |
// if (!resultSet.hasNext()) { | |
// ctx.fail(404); | |
// } else { | |
// let row = resultSet.next(); | |
// ctx.response() | |
// .putHeader("Content-Type", "application/json") | |
// .end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)})); | |
// } | |
// }) | |
// .catch(e => ctx.fail(e)); | |
// } | |
// // Step #3 | |
// ctx => { | |
// (async function () { | |
// // vert.x promise | |
// let postgres = util.promisify(client); | |
// | |
// try { | |
// let rs = await postgres.preparedQuery(SELECT_WORLD, Tuple.of(1)); | |
// let resultSet = rs.iterator(); | |
// | |
// if (!resultSet.hasNext()) { | |
// ctx.fail(404); | |
// } else { | |
// let row = resultSet.next(); | |
// ctx.response() | |
// .putHeader("Content-Type", "application/json") | |
// .end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)})); | |
// } | |
// } catch (e) { | |
// ctx.fail(e); | |
// } | |
// })(); | |
// } | |
// Step #4 | |
async ctx => { | |
// vert.x promise | |
let postgres = util.promisify(client); | |
try { | |
let rs = await postgres.preparedQuery(SELECT_WORLD, Tuple.of(1)); | |
let resultSet = rs.iterator(); | |
if (!resultSet.hasNext()) { | |
ctx.fail(404); | |
} else { | |
let row = resultSet.next(); | |
ctx.response() | |
.putHeader("Content-Type", "application/json") | |
.end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)})); | |
} | |
} catch (e) { | |
ctx.fail(e); | |
} | |
} | |
); | |
vertx | |
.createHttpServer() | |
.requestHandler(req => app.accept(req)) | |
.listen(8080); | |
console.log('Server listening at: http://localhost:8080/'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "async-project", | |
"version": "1.0.0", | |
"private": true, | |
"description": "Example of an async application", | |
"main": "index.js", | |
"scripts": { | |
"postinstall": "vertx-scripts init", | |
"start": "vertx-scripts launcher run", | |
"package": "vertx-scripts package", | |
"repl": "vertx-scripts repl" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"vertx-scripts": "latest" | |
}, | |
"dependencies": { | |
"@vertx/core": "latest", | |
"@vertx/web": "latest", | |
"@reactiverse/reactive-pg-client": "latest" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment