{"size": 1260, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "dapino/escuelajs-reto-03", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "dapino/escuelajs-reto-03", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "dapino/escuelajs-reto-03", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 38.1818181818, "max_line_length": 94, "alphanum_fraction": 0.6277777778}
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const API_URL = 'https://rickandmortyapi.com/api/character/';
const xhttp = new XMLHttpRequest();

const fetchDataFromAPI = url => {
    return new Promise((resolve, reject) => {
        xhttp.onreadystatechange = () => {
            const response = JSON.parse(xhttp.responseText)
            xhttp.status === 200 ? resolve(response) : reject(new Error(`${xhttp.status}`));
        };
        xhttp.open("GET", url, false);
        xhttp.send();
    })
};

const getDataFromAPI = async API_URL => {
    try {
        console.log("First fetch...");
        const wholeData = await fetchDataFromAPI(API_URL);
        console.log("Second fetch...");
        const firstCharacter = await fetchDataFromAPI(`${API_URL}${wholeData.results[0].id}`);
        console.log("Third fetch...");
        const characterOrigin = await fetchDataFromAPI(`${firstCharacter.origin.url}`);
        console.log(`There are ${wholeData.info.count} living forms in the universe`);
        console.log(`Hi Morty, burp!, i'm ${firstCharacter.name}`);
        console.log(`burp!, i'm from ${characterOrigin.name}`);
    } catch (err) {
        console.log(`Something is wrong!! | ${err}`)
    }
}
getDataFromAPI(API_URL)

