Skip to content

Instantly share code, notes, and snippets.

@weihungchin
Last active January 16, 2021 11:23
Show Gist options
  • Save weihungchin/8486aab986c90941535da6d202d47870 to your computer and use it in GitHub Desktop.
Save weihungchin/8486aab986c90941535da6d202d47870 to your computer and use it in GitHub Desktop.
Fetch wih Rxjs
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