Last active
May 16, 2019 10:32
-
-
Save klemola/e37a2cde2c333568a9e5ebf606cf328a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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