xgqfrms / learning

learning : A collection of all kinds of resources, videos, pdf, blogs, codes... 📚 + 💻 + ❤
https://learning.xgqfrms.xyz
MIT License
16 stars 12 forks source link

Fetch API & Async Await #52

Open xgqfrms opened 5 years ago

xgqfrms commented 5 years ago

Fetch API & Async Await

practical demo

const fetchJSON = (url = ``) => {
    return fetch(url,
        {
            method: "GET",
            // mode: "no-cors",
            mode: "cors",
            credentials: "same-origin",
            headers: {
                "Content-Type": "application/json; charset=utf-8",
            },
        })
        .then(res => res.json())
        .then(
            (json) => {
                return json;
            }
        )
        .catch(err => console.log(`fetch error`, err));
};

// async / await
async function getDatas(url = ``) {
    try {
        return await fetchJSON(url);
    } catch (err) {
        console.error("getDatas error:\n", err);
    }
}

xgqfrms commented 5 years ago
"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 *
 * @description fetchAPIs
 * @augments
 * @example
 * @link
 *
 */

const getToken = (url = ``, debug = false) => {
    let log = console.log;
    let error = console.error;
    if (url) {
        return fetch(url, {
            method: "GET",
            cors: "no",
        })
        .then(res => res.json())
        .then(json => {
            if (debug) {
                log(`JSON =`, JSON.stringify(json, null, 4));
            }
            let Token = ``;
            let {
                Result,
                Code,
            } = json;
            if (Code === 200) {
                Token = Result[0].Token;
                DD_Token = Token;
            } else {
                DD_Token = "";
            }
        })
        .catch(err => {
            error(`fetch error`, err);
        });
    } else {
        error(`fetch url is empty!`);
        return ``;
    }
};

const fetchPOST = (options = {}, debug = false) => {
    let log = console.log;
    let error = console.error;
    let {
        url,
        token,
    } = options;
    let parmas = {
        "Type": "attention",
        "Batch": 0,
        "Token": token,
    };
    return fetch(url, {
        method: "POST",
        cors: "no",
        body: JSON.stringify(parmas),
    })
    .then(res => res.json())
    .then(json => {
        if (!debug) {
            log(`JSON =`, JSON.stringify(json, null, 4));
        }
    })
    .catch(err => {
        error(`fetch error`, err);
    });
};

const fetchGET = (options = {}, debug = false) => {
    let log = console.log;
    let error = console.error;
    let {
        url,
        token,
    } = options;
    const URL = `${url}?token=${token}`;
    return fetch(URL, {
        method: "GET",
        cors: "no",
    })
    .then(res => res.json())
    .then(json => {
        if (!debug) {
            log(`JSON =`, JSON.stringify(json, null, 4));
        }
    })
    .catch(err => {
        error(`fetch error`, err);
    });
};

const fetchAPIs = {
    getToken,
    fetchPOST,
    fetchGET,
};

export default fetchAPIs;

export {
    getToken,
    fetchPOST,
    fetchGET,
};
xgqfrms commented 5 years ago

https://www.cnblogs.com/xgqfrms/p/10766432.html