npm.io
0.3.5 • Published 4 years ago

k-xhr

Licence
MIT
Version
0.3.5
Deps
0
Size
9 kB
Vulns
0
Weekly
0

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
  });

Keywords