zoukankan      html  css  js  c++  java
  • 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));
    });
  • 相关阅读:
    C#
    if
    .net 5.0
    C# 未提供必须形参对应的实参
    EasyRTC报错 “[ERR] mod_local_stream.c:880 Unknown source default”排查
    EasyRTC通过公网进入会议室失败问题排查及解决
    【CF1558B】Up the Strip
    【AT Keyence2019E】Connecting Cities
    【洛谷P7470】岛屿探险
    【洛谷P6628】丁香之路
  • 原文地址:https://www.cnblogs.com/sunupo/p/15538986.html
Copyright © 2011-2022 走看看