-
-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how does one read a file in a jsx widget? #526
Comments
Hi! The most important thing you need to understand about programming in JavaScript is that I/O operations, like reading a file or making a web request, are asynchronous - when your code makes the call to read a file or whatever, it cannot pause to wait for the result. Instead, you provide some additional code describing what to do when the result does arrive. For example, This arrangement can be a pain to work with directly, so there are two layers of helpfulness on top of the basic asynchronous operation of the language. The first is called promises: when you call function doThreeThings() {
return doSomething()
.then(result => doAnotherThingWith(result))
.then(anotherResult => doThirdThingWith(anotherResult));
} In order to run those three operations in sequence and get the result from the third. However, promises can still be verbose and inconvenient for some kinds of code. So the second layer of helpfulness is called an async function doThreeThings() {
var result = await doSomething();
var anotherResult = await doAnotherThingWith(result);
return doThirdThingWith(anotherResult);
} Using So, with that context covered, let's talk about your situation. The reason Fortunately, run(`cat /Users/kvaradhan/${keyFile}`).then(apiKey => console.log(apiKey)); Of course, you probably want to use the API key once you have it. I don't know what your weather API looks like, so I can't give you an exact example, but here's a really basic working example of making a simple HTTP call and displaying its results: import { run } from 'uebersicht';
const keyPath = '~/my-api-key-file';
const keyAsync = run(`cat ${keyPath}`);
export const refreshFrequency = 1000 * 60; // run once per minute
export const command = async () => {
const key = await keyAsync;
const response = await fetch('https://httpbin.org/headers', {headers: {Authorization: `Bearer ${key}`}})
return response.json();
};
export const className = {
bottom: 0, right: 0,
width: 800, height: 300,
backgroundColor: 'white',
whiteSpace: 'pre-wrap',
};
export const render = ({output}) => <code>{JSON.stringify(output.headers, null, 2)}</code>; Hopefully that's enough to get you going! 💖 |
I am not a react/node/java coder at all and am muddling through by example here, so please bear with me if this is a stupid question.
I am trying to write a weather app, index.jsx, and dont want to encode the API key in the file, checked into github.
What would be the best way to do this?
It didn't, i get this on the console log:
and this is on the console log:
Q: Is there a good working example of how to do this? or how I might proceed?
Thanks,
Kannan
The text was updated successfully, but these errors were encountered: