Created
March 8, 2018 06:32
-
-
Save kitasuna/e99cbed63a2fc07d496e811b68e0b96e to your computer and use it in GitHub Desktop.
Different fluture patterns
This file contains 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 axios from 'axios' | |
import * as R from 'ramda' | |
import * as Future from 'fluture' | |
import { Promise } from 'es6-promise' | |
import { AxiosResponse, AxiosError } from "axios"; | |
interface Post { | |
id: number; | |
userId?: number; | |
title: string; | |
body: string; | |
user?: User; | |
} | |
interface User { | |
id: number; | |
name: string; | |
username: string; | |
email: string; | |
} | |
const getPost = (postId: number) => { | |
return Future.tryP( () => axios | |
.get('https://jsonplaceholder.typicode.com/posts/' + postId.toString()) | |
.then((post: AxiosResponse) => { | |
return post.data | |
}) | |
.catch((err: AxiosError) => { | |
return 'Error talking to `posts` endpoint' | |
}) | |
); | |
} | |
const getPosts = () => { | |
return Future.tryP( () => axios | |
.get('https://jsonplaceholder.typicode.com/posts') | |
.then((post: AxiosResponse) => { | |
return post.data | |
}) | |
.catch((err: AxiosError) => { | |
return 'Error talking to `posts` endpoint' | |
}) | |
); | |
} | |
const getUserThrows = (post) => { | |
return Future.tryP (() => axios | |
.get<User>('https://jsonplaceholder.typicode.com/users/' + post.userId) | |
.then((eUser: AxiosResponse<User>) => { | |
throw 'woooooops!' | |
}) | |
.catch((err: AxiosError) => { | |
return 'Error talking to `users` endpoint for id: ' + post.id | |
}) | |
) | |
} | |
const getUser = (post) => { | |
return Future.tryP (() => axios | |
.get<User>('https://jsonplaceholder.typicode.com/users/' + post.userId) | |
.then((eUser: AxiosResponse<User>) => { | |
return {...post, user: eUser.data} | |
}) | |
.catch((err: AxiosError) => { | |
return 'Error talking to `users` endpoint for id: ' + post.id | |
}) | |
) | |
} | |
const getReject = () => { | |
return Future.tryP( () => axios | |
.get<User>('https://jsonplaceholder.typicode.com/users/1') | |
.then((eUser: AxiosResponse<User>) => { | |
throw 'some error' | |
}) | |
) | |
} | |
const printErr = (err) => {console.log('In fork error handler', err)}; | |
const printRes = (res) => {console.log('In fork resolve handler'); console.dir(res);}; | |
// Just getPost | |
getPost(1) | |
.fork( | |
printErr, | |
printRes | |
) | |
// Just getReject | |
// | |
getReject() | |
.fork( | |
printErr, | |
printRes | |
) | |
/* | |
* getPost chained with getUser, compose responses | |
*/ | |
const getJustOne = getPost(1) | |
.chain(post => getUser(post)) | |
getJustOne | |
.fork( | |
printErr, | |
printRes | |
) | |
/* | |
* multiple posts, get user for each, run getUser calls in batches of 10, compose responses | |
* ... this takes time to run, so commenting it out | |
*/ | |
/* | |
getPosts() | |
.chain((posts) => Future.parallel(10, R.map(getUser, posts))) | |
.fork( | |
printErr, | |
printRes | |
) | |
*/ | |
/* | |
* Two independent calls | |
*/ | |
const getPostOne = getPost(1) | |
.chain(post => getUser(post)) | |
const getPostTwo = getPost(2) | |
.chain(post => getUser(post)) | |
const getTwo = | |
Future.parallel(2, [getPostOne, getPostTwo]) | |
.fork( | |
printErr, | |
arr => printRes({post1: arr[0], post2: arr[1]}) | |
) | |
/* | |
* getReject (thrower) chained with getUser | |
*/ | |
getReject() | |
.chain(post => getUser(post)) | |
.fork( | |
printErr, | |
printRes | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment