Last active
January 16, 2021 11:23
-
-
Save weihungchin/8486aab986c90941535da6d202d47870 to your computer and use it in GitHub Desktop.
Fetch wih Rxjs
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
import { tap } from "rxjs/operators"; | |
import { zip } from "rxjs"; | |
function getUser(id) { | |
return fetch(`https://jsonplaceholder.typicode.com/users/${id}`).then(res => | |
res.json() | |
); | |
} | |
function getPost(userId) { | |
return fetch(`https://jsonplaceholder.typicode.com/postss/${userId}`).then( | |
res => res.json() | |
); | |
} | |
function makeConcurrentRequests(userId, postId) { | |
zip(getUser(userId), getPost(postId)) // we can pass in promises directly | |
.subscribe( | |
res => { | |
const user = res[0]; // getUser response | |
const post = res[1]; // getPost response | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment