Skip to content

Instantly share code, notes, and snippets.

View younho9's full-sized avatar
👨‍💻

Younho Choo younho9

👨‍💻
View GitHub Profile
@younho9
younho9 / mock-http.js
Created July 2, 2021 01:06 — forked from jofftiquez/mock-http.js
Mock HTTP request in javascript using promise and timeout.
const mock = (success, timeout) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(success) {
resolve();
} else {
reject({message: 'Error'});
}
}, timeout);
});
@younho9
younho9 / fetch.ts
Created May 12, 2021 07:48
Fetch Error Handling
const handleErrors = (res: Response) => {
if (!res.ok) {
throw Error(res.statusText);
}
return res;
}
fetch("http://httpstat.us/500")
.then(handleErrors)
const filterObj = (obj, f) => (o=Object).fromEntries(o.entries(obj).filter([k,v]=>f(k)))
const select = (obj, ...props) => filterObj(obj, k => props.includes(k))
const omit = (obj, ...props) => filterObj(obj, k => !props.includes(k))