Skip to content

Instantly share code, notes, and snippets.

@klemola
Last active May 16, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klemola/e37a2cde2c333568a9e5ebf606cf328a to your computer and use it in GitHub Desktop.
Save klemola/e37a2cde2c333568a9e5ebf606cf328a to your computer and use it in GitHub Desktop.
interface User {
username: string;
email: string;
}
export function main() {
let isLoading = true;
let fetchFailed = false;
let data: User;
fetchData()
.then(response => {
isLoading = false;
data = response;
})
.catch(error => {
isLoading = false;
fetchFailed = true;
});
}
function render(isLoading, data, fetchFailed) {
if (isLoading) {
return "Loading...";
} else if (!isLoading && data) {
return `Hello, ${data.username}`;
} else if (fetchFailed) {
return "Could not fetch data!";
}
}
// simulated fetch
function fetchData() {
return new Promise<User>(resolve =>
resolve({
username: "Tim Testuser",
email: "tim@example.com"
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment