async/wait使用fetch 与generator使用fetch

1.

// Example POST method implementation:
async function postData(url='15479453.html', data={}) {
    // Default options are marked with *
    const response = await fetch(url, {
        method: 'POST',
        // *GET, POST, PUT, DELETE, etc.
        mode: 'cors',
        // no-cors, *cors, same-origin
        cache: 'no-cache',
        // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin',
        // include, *same-origin, omit
        headers: {
            'Content-Type': 'application/json'// 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: 'follow',
        // manual, *follow, error
        referrerPolicy: 'no-referrer',
        // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data)// body data type must match "Content-Type" header
    });
    return response.text();
    // parses JSON response into native JavaScript objects
}

postData('15479453.html', {
    answer: 42
}).then(data=>{
    console.log(data);
    // JSON data parsed by `data.json()` call
}
);

2.

function *gen() {
    // var url = 'https://api.github.com/users/github';
    var url = 'https//:www.baidu.com';
    var result = (yield fetch("15479453.html"));
    console.log("result
", result.slice(0, 20));
    return result;
}
var g = gen();
var result = g.next();
console.log("g.next()")

result.value.then(function(data) {
    return data.text();
}).then(function(data) {
    console.log("g.next(data)
", g.next(data));
});
原文地址:https://www.cnblogs.com/sunupo/p/15538986.html