Skip to content

Instantly share code, notes, and snippets.

@willie-hung
Last active July 12, 2023 18:46
Show Gist options
  • Save willie-hung/8c9849dd4846a8bc5555fe31c2676284 to your computer and use it in GitHub Desktop.
Save willie-hung/8c9849dd4846a8bc5555fe31c2676284 to your computer and use it in GitHub Desktop.
post_6
function fetchUser(userId: number): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (userId === 1) {
resolve("Sung-Jie Hung");
} else {
reject("User not found");
}
}, 2000);
});
}
function fetchUserPosts(user: string): Promise<string[]> {
return new Promise((resolve) => {
setTimeout(() => {
const posts = ["Post 1", "Post 2", "Post 3"];
resolve(posts);
}, 1500);
});
}
fetchUser(1)
.then((user) => {
console.log("User:", user);
return fetchUserPosts(user);
})
.then((posts) => {
console.log("User Posts:", posts);
})
.catch((error) => {
console.error("Error:", error);
});
// Output:
// User: Sung-Jie Hung
// User Posts: [ 'Post 1', 'Post 2', 'Post 3' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment