Hello! Since I have a few minutes until my next pomodoro, I decided to share this tip.
In case you make or find an asynchronous function working at node or browser and want to convert it into a context.async function… here’s an example!
Before
// declaring it
const requestToGetPlainText = async (fileUrl) => {
let response = await fetch(fileUrl);
let plainText = await response.text();
return plainText
}
// calling it
requestToGetPlainText("https://example.com/myFile.csv")
After
// this declares AND calls it at the same time
let attainedPlainText = context.async(async callback => {
try {
let response = await fetch(properties.file_url);
let plainText = await response.text();
callback(null, plainText);
}
catch (err) {
callback(err);
}
});
// this returns it from server side
// but you could publish attainedPlainText to
// a custom element state if client side
return {
attained_plain_text: attainedPlainText
}
Case you’re wondering how to use fetch within node, just grab this node package:
const fetch = require('cross-fetch');
Happy Bubbling!