可以设置超时版的的fetch


        // 超时版的fetch
        _fetch(fetch, timeout) {
            return Promise.race([
                fetch,
                new Promise(function (resolve, reject) {
                    setTimeout(() => reject(new Error('request timeout')), timeout);
                })
            ]);
        }

        // 使用
        _fetch(fetch('url'), 1000).then((info)=> {
            return info.text();
        }).then((info)=> {
            console.log(info);
        }).catch((err)=> {
            throw new Error(err);    
        });

 

        _fetch(fetch(url, {
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: `key1=value1&key2=value2&key3=value3...`
        }), 10000).then((info)=> {
            return info.text();
        }).then((info)=> {
            console.log(info);
        }).catch((err)=> {
            throw new Error(err);
        });

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM