js Promise fetch demo

时间:2023-03-10 00:03:00
js Promise fetch demo
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
function worker(url, url_2, i){
return new Promise(function(resolve, reject){
fetch(url).then(function(response_out) {
response_out.text().then(
function(outer_text){
console.log(i + " 外部完成");
fetch(url_2).then(function(response_in) {
var inner_text = response_in.text().then(function(inner_text){
console.log(i + " 内部完成");
resolve( outer_text + " + " + inner_text);
});
});
}
);
})
});
} var worker_list = [];
for (var i=0; i<10; i++){
var url = 'data.txt';
var url_2 = 'data2.txt';
var w = worker(url, url_2, i);
worker_list.push(w);
} Promise.all(worker_list).then(function(all_result_list){
console.log("都完成了");
console.log(all_result_list);
});
</script>
</html>