0.3.5 • Published 2 years ago
k-xhr v0.3.5
k-xhr
Lightweight XMLHttpRequest, 0.2KB gzipped, Promise compatible, No dependencies, IE11+.
Thanks to JSONPlaceholder for their open API which I used for testing.
Installation
npm install k-xhr
Quick Start
import kxhr from "k-xhr";
const data = { id: 1, data: "Testing string" };
kxhr("https://jsonplaceholder.typicode.com/posts", "post", JSON.stringify(data), {
contentType: "application/json"
}).then((res) => {
const json = JSON.parse(res);
console.log(json.id);
console.log(json.data);
});
Sequential Catch
let i = 0;
kxhr("https://jsonplaceholder.typicode.com/todos/1")
.then(() => {
++i; // i = 1
throw new Error("catch 1");
})
.catch((e) => {
++i; // i = 2
})
.then(() => {
++i; // i = 3
throw new Error("catch 2");
})
.catch((e) => {
++i; // i = 4
})
.finally(() => {
console.log(i); // 4
});