How to get a value in async function as soon as possible? - javascript

I'm working with Ethereum blockchain, but my problem my is JavaScript (async, await function).
Here my code simplified:
In my html
App.addBlockChain(n.username,n.first,n.last,n.email).then(value => {
**//here I need the hash of my transaction**
}).catch(error => {
alert("Errore: " + error );
});
In my App.js file
addBlockChain: async(u,n,c,e) => {
let hash;
const web3 = new Web3(App.web3Provider);
const signed = await web3.eth.accounts.signTransaction(options, account.privateKey);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction)
.on('transactionHash', function(hash_returned){
//I need this hash hash_returned as soon as possible in my html ***
hash= hash_returned;
})
.on('receipt', function(receipt){... })
.on('confirmation', function(confirmationNumber, receipt){ ... })
.on('error', console.error); // If a out of gas error, the second parameter is the receipt.;
return hash; //it is returned only when on('confirmation') is terminated
Any help with any code of example?
Thanks a lot in advance.

Welcome to the fantastic world of asynchronism... One way to do this would be :
const hash_returned = await App.addBlockChain(n.username, n.first, n.last, n.email);
and in your App class :
addBlockChain: async(u, n, c, e) => {
const web3 = new Web3(App.web3Provider);
const signed = await web3.eth.accounts.signTransaction(options, account.privateKey);
return new Promise(resolve => { // addBlockChain must return a Promise, so it can be "await"ed
web3.eth.sendSignedTransaction(signed.rawTransaction)
.on('transactionHash', function(hash_returned) {
resolve(hash_returned); // now that you have hash_returned, you can return it by resolving the Promise with it
})
// or more simply (equivalent) :
// .on('transactionHash', resolve)
})
}

Related

BLE keep connection alive (website / pwa) using javascript

Disclaimer: I'm a noob coder.
I'm trying to keep the connection alive until the session ends (the user closes the browser/navigates to something else)
the code here shows the modified sample by Chrome Samples
function readData() {
console.log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice({
filters: [{
services: [serviceUuid]
}]
})
.then(device => {
return device.gatt.connect();
})
.then(server => {
return server.getPrimaryService(serviceUuid);
})
.then(service => {
return service.getCharacteristic(characterUuid);
})
.then(characteristic => characteristic.readValue())
.then(result => decodeValues(result))
.then(result => document.getElementById("output").innerHTML = result)
.catch(error => {
console.log('Argh! ' + error);
});
}
but the issue here is, if I want to write to the same service of a different characteristic when the user clicks another button, I have to use navigator.bluetooth.requestDevice(...) again which will bring up the popup dialog to choose the device. This will become very unintuitive when using the site.
Please help.
Thank You.
So, it worked using async await .....
explanation for the below code ...
I'm gonna call the readData first before writeData .... hence storing service in a global variable
let device, server, service;
let serviceUuid = '4fafc201-1fb5-459e-8fcc-c5c9c331914b';
let writeUuid = 'beb5483e-36e1-4688-b7f5-ea07361b26a8';
let readUuid = '9fde8759-ffd6-40d7-a50e-f0ffa74abd25';
async function readData() {
device = await navigator.bluetooth.requestDevice({ filters: [{ services: [serviceUuid] }] })
.catch(error => console.log(error));
server = await device.gatt.connect().catch(error => console.log(error));
service = await server.getPrimaryService(serviceUuid).catch(error => console.log(error));
let characteristic = await service.getCharacteristics(readUuid).catch(error => console.log(error));
let value = decodeValues(await characteristic[0].readValue().catch(error => console.log(error)));
document.getElementById('time-left').innerHTML = value;
}
async function writeData() {
let characteristic = await service.getCharacteristics(writeUuid).catch(error => console.log(error));
characteristic[0].writeValue(encodeValues('5'));
}
function decodeValues(result) {
let decoder = new TextDecoder('utf-8');
return decoder.decode(result.buffer);
}
function encodeValues(stringValue) {
let encoder = new TextEncoder('utf-8');
return encoder.encode(stringValue);
}
hope this helps for anyone searching for the answer

How can I "encapsulate" this code into a module so it could become reusable?

I have got this Node.JS snippet and would like to write it as a module, so I can use recaptcha in different parts of my system.
This is how it currently looks like:
app.post('/register_user', (req, res) => {
const secret_key = process.env.RECAPTCHA_SECRET;
const token = req.body.recaptcha;
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${secret_key}&response=${token}`;
fetch(url, { method: "post",})
.then((response) => response.json())
.then((google_response) => {
if (google_response.success == true) {
res.format({'text/html': () => res.redirect(303, '/register'),})
} else {
return res.send({ response: "Failed" });
}
})
.catch((error) => {
return res.json({ error });
});
})
I have tried to write the following module which works absolutely great, but I have absolute no idea about how to call it from the app.post, since I always get undefined as return:
import fetch from 'node-fetch';
export function fetch_out(url, timeout = 7000) {
return Promise.race([
fetch(url),
new Promise((_, reject) =>
setTimeout(() => reject(new Error('timeout')), timeout)
)
]);
}
export async function checkRecaptcha(token, secret_key){
const url = "https://www.google.com/recaptcha/api/siteverify?secret=" + secret_key + "&response=" + token;
try{
const response = await fetch_out(url, 1000);
const google_response = await response.json();
}catch(error){
return error;
}
return google_response;
}
Any help would be appreciated! Thanks!
You could make this method reusable by removing the framework actions that need to happen and only return if the validation was successful or not. This way, it will be reusable in another project that doesn't use a specific framework.
Example module;
export async function checkRecaptcha(token, secret_key) {
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${secret_key}&response=${token}`;
const response = await fetch(url, { method: "post",});
if (!response.ok) return false;
const json = await response.json();
if (!json.success) return false;
return true;
}
Usage:
import { checkRecaptcha } from "./some-file-name";
app.post('/register_user', async (req, res) => {
const isHuman = await checkRecaptcha(req.body.recaptcha, process.env.RECAPTCHA_SECRET);
if (!isHuman) {
return res.send({ response: "Failed" });
}
return res.format({'text/html': () => res.redirect(303, '/register'),});
});
If you specifically want to call an action after the validation, you can also use successful and error callbacks.

AWS Secrets Manager and javascript

I have about 7 hours and dozens of try's to get a AWS Secret. I have followed the AWS example code as well as other examples found here and on other sites. My current problem is the code returns a Promise in my variable 'p', not the plaintext secret string. I'm pulling my hair out here... If it is returning a promise while still pending a resolve; I don't know why it does not wait. The calling code is in a sequelize.config file and not wrapped in an exported function--thus the async function () => below.
getSecrets.js
const AWS = require ('aws-sdk')
const sm = new AWS.SecretsManager({region: 'us-east-1'});
export default async function getSecret (secretId) {
const data = await sm.getSecretValue({
SecretId: secretId
})
.promise();
console.log ("returning SecretString:", data.SecretString)
return data.SecretString;
Calling code:
if (RUN_ENV != 'local') {
const p = async function () {
await getSecrets (DB_PASSWORD)
.then (function ( value ) {
return value;
})
.catch ((error) => {
console.log ("AWS getSecrets error: ", error);
})
}
password = p;
}
Here's a simple way to get secret strings from Secrets Manager:
const AWS = require('aws-sdk');
const client = new AWS.SecretsManager({ region: "us-east-1" });
const getMySecret = async (SecretId) => {
const s = await client.getSecretValue({ SecretId }).promise();
return s.SecretString;
};
// Async IIFE
(async() => {
const secret_101 = await getMySecret('secret-101');
console.log('My secret:', secret_101);
})();
Your original problem, as #GalAbra suggested, is that your code has password = p when it needs to be const password = p(). Specifically, rather than taking a reference to the function, it needs to invoke the function.

I get Promise { <pending> } as returned value and also calling in async scope gives me undefined immediately

Im trying to return a value from a Promise in async-await form and use it in another function in another file, but I do have problem because my Promise doesnt return any value.
When im trying to console.log('website') it returns me undefined immediately (it's like the value is not being fetched at all from API services). I dont know what im doing wrong, I really love to learn about Promises and Async-Await but each time im trying to work with them im getting more confused.
const dns = require('dns')
const iplocation = require("iplocation").default;
const emojiFlags = require('emoji-flags');
const getServerIPAddress = async (server) => {
return new Promise((resolve, reject) => {
dns.lookup(server, (err, address) => {
if (err) throw reject(err);
resolve(address);
});
});
};
const getServerLocation = async (server) => {
const ip = await getServerIPAddress(server)
iplocation(ip).then((res) => {
const country = emojiFlags.countryCode(res.countryCode)
const result = `Location: ${country.emoji} ${country.name}`
return result
})
.catch(err => {
return `Location: Unknown`
});
}
(async function() {
console.log(await getServerLocation('www.google.com'))
})()
module.exports = {
getServerLocation
}
It is really important for me to get result from this function first, then use its value in another function. I wish you could give me tips on how to do tasks asynchronously.
You're clearly using async so it's not apparent why you're using then as well. If you use then then you must return the promise as well in order to preserve the promise chain:
const getServerLocation = async (server) => {
const ip = await getServerIPAddress(server)
return iplocation(ip).then((res) => {
const country = emojiFlags.countryCode(res.countryCode)
const result = `Location: ${country.emoji} ${country.name}`
return result
})
.catch(err => {
return `Location: Unknown`
});
}
Otherwise just async this:
const getServerLocation = async (server) => {
const ip = await getServerIPAddress(server)
let res = await iplocation(ip);
const country = emojiFlags.countryCode(res.countryCode)
const result = `Location: ${country.emoji} ${country.name}`
return result
}
const getServerLocation = async (server) => {
const ip = await getServerIPAddress(server)
//you need to return
return iplocation(ip).then((res) => {
const country = emojiFlags.countryCode(res.countryCode)
const result = `Location: ${country.emoji} ${country.name}`
return result
})
.catch(err => {
return `Location: Unknown`
});
}

How to write an async function that resolves when `data` event emitter fires

I am using node-serialport to communicate with a piece of hardware. It just writes a command and receives a response.
https://serialport.io/docs/en/api-parsers-overview
The following code works:
const port = new SerialPort(path);
const parser = port.pipe(new Readline({ delimiter: '\r', encoding: 'ascii' }));
const requestArray = [];
parser.on('data', (data) => {
// get first item in array
const request = requestArray[0];
// remove first item
requestArray.shift();
// resolve promise
request.promise.resolve(data);
});
export const getFirmwareVersion = async () => {
let resolvePromise;
let rejectPromise;
const promise = new Promise((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
const title = 'getFirmwareVersion';
const cmd = 'V\r';
requestArray.push({
title,
cmd,
promise: {
resolve: resolvePromise,
reject: rejectPromise
}
});
await v2Port.write(cmd);
return promise;
};
Then from my app (which is written in electron/react) I can call the function:
<Button onClick={() => {
let data = await _api.getFirmwareVersion();
console.log('done waiting...');
console.log(data);
}>
Click Me
</Button>
Is there anyway I can refactor this code to make it more succinct?
Is there a way to get the Promise from the async function, rather than having to make a new Promise?
Is there a way to tap into the Transform Stream that already exists and pipe the Promise in there somehow?
I'm also new to async/await, and wanted to avoid using callbacks, especially in the React/Redux side of things.
I aim to have a lot of these endpoints for the api (i.e. getFirmwareVersion, getTemperature, etc...). So I want to make the code as concise as possible. I don't want the UI to have any underlying knowledge of how the API is getting the data. It just needs to request it like any other API and wait for a response.
Oh, I think I get it. The parser is receiving data constantly. So when a request comes, you wait for the next data and send it when it arrives. I suggest you to write an intermediate class.
Like this:
const SerialPort = require('serialport')
const Readline = require('#serialport/parser-readline')
const { EventEmitter } = require('events');
class SerialPortListener extends EventEmitter {
constructor(path) {
super();
this.serialPortPath = path;
}
init() {
this.serialPort = new SerialPort(this.serialPortPath);
const parser = this.serialPort.pipe(new Readline({ delimiter: '\r', encoding: 'ascii' }));
parser.on('data', data => this.emit('data', data));
}
}
Then you could modify the getFirmwareVersion like this:
const serialPortListener = new SerialPortListener(path);
serialPortListener.init();
export const getFirmwareVersion = () => {
return new Promise((resolve, reject) => {
serialPortListener.once('data', async (data) => {
try {
const cmd = 'V\r';
await v2Port.write(cmd);
resolve(data);
} catch (ex) {
reject(ex);
}
});
});
};
Based on help from Mehmet, here is what I ended up with:
const _port = new SerialPort(path);
const _parser = _port.pipe(new Readline({ delimiter: '\r', encoding: 'ascii' }));
const waitForData = async () => {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => reject('Write Timeout'), 500);
_parser.once('data', (data) => {
clearTimeout(timeoutId);
resolve(data);
});
});
};
const createAPIFunction = (cmdTemplate, validationString) => {
return async (config) => {
try {
// replace {key} in template with config[key] props
const cmd = cmdTemplate.replace(/{(\w+)}/g, (_, key) => {
return config[key];
});
_port.write(cmd + '\r');
const data = await waitForData();
// validate data
if (data.startsWith(validationString)) {
// is valid
return data;
} else {
// invalid data
throw new Error('Invalid Data Returned');
}
} catch (err) {
throw err;
}
};
};
export const getFirmwareVersion = createAPIFunction('V', 'V1');
export const enableSampling = createAPIFunction('G1{scope}', 'G11');

Categories