const { promisify } = require('util');
/* ... */
const query = promisify(connection.query).bind(connection);
const commit = promisify(connection.commit).bind(connection);
const rollback = promisify(connection.rollback).bind(connection);
const beginTransaction = promisify(connection.beginTransaction).bind(connection);
/* ... */
async function run(title) {
try {
await beginTransaction();
const { insertId } = await query('INSERT INTO posts SET title=?', title);
const log = `Post ${insertId} added`;
await query('INSERT INTO log SET data=?', log);
await commit();
console.log('Success!');
} catch (e) {
await rollback();
}
}
const fetch = require('node-fetch');
const SocksProxyAgent = require('socks-proxy-agent');
const { URL } = require('url');
const proxy = process.env.socks_proxy || 'socks://127.0.0.1:9050';
const endpoint = process.argv[2] || 'http://nodejs.org/api/';
console.log('using proxy server', proxy);
console.log('attempting to GET', endpoint);
const agent = new SocksProxyAgent(proxy);
fetch(endpoint, { agent })
.then(response => response.text())
.then((text) => {
console.log('Response:', text);
})
.catch(console.error);