Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 1.14 KB

File metadata and controls

36 lines (27 loc) · 1.14 KB

_fetch

This is a proxy function. It uses the same simple-to-read syntax as the fetch API but is the good ol' XMLHttpRequest under the hood.
This was an attempt to help my friend @ericklaus16 on the mobile version of AlfaChat.

I hope it helps!

Syntax

As previously mentioned, it follows the same syntax as the fetch JavaScript API.

Request example:

_fetch("https://google.com", {
    mode: "POST",
    headers: {
        'Content-Type': 'application/json'
    },
    body: null
});

It returns a promise which is resolved as soon as the XMLHttpRequest response loads. Response example:

_fetch().then(res => {
    console.log(this);  // XMLHttpRequest object
    console.log(res);   // Response from the server
});

Note #1: there's no way of getting the original's fetch API responses (e.g Response.ok or Response.status) since it isn't a fetch call. However, it is possible to access the XMLHttpRequest object properties through the this keyword.

Note #2: one can include Headers on the options parameter (the second one) as when using the fetch API.