【AJAX】通过fetch拿到数据

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Asynchronous JavaScript</title>
</head>
<body>
    <h1>Asynchronous JavaScript</h1>
    <script>
    	/*
    	const second = () => {
    		setTimeout((str)=>{
    			console.log(str);
    		}, 1000, "Here is the second one");
    	}

        const first = () => {
        	console.log('First begin');
        	second();
        	console.log('The end');
        }

        first();
		*/
		
		// Callback hell

		/*
		function getRecipe () {
			const recipeID = [1, 2, 3, 4];
			setTimeout((recipeID) => {
				console.log(...recipeID);

				setTimeout((id) => {
					const recipe = {
						"title" : 'French tomato pasta',
						"publisher" : "youheng"
					};
					console.log(`${id} : ${recipe.title}`);

					setTimeout(publisher=>{
						const recipe = {
							"title": "Italian Pizza",
							"publisher" : "youheng"
						};
						console.log(`${recipe.title} : ${publisher}`);
					}, 1000, recipe.publisher);

				}, 1000, recipeID[2]);

			}, 1500, recipeID);
		}

		getRecipe();
		*/

		
		const getIDs = new Promise((resolve, reject) =>{
			setTimeout(() => {
				const ID = [1321, 2132, 5323, 6434];
				// success situation
				resolve(ID);
				// failure situation
				reject("error here");
			}, 1000);
		});

		const getRecipe = (recID) => {
			return new Promise((solve, reject) => {
				setTimeout(() => {
					const recipe = {
						"title" : "recipe menu",
						"publisher" : "youheng"
					};
					let data = {
						"id" : recID,
						"publisher" : recipe.publisher
					}
					solve(data);
				}, 1000, recID);
			});
		}

		const getRelated = publisher => {
			return new Promise((solve, reject)=>{
				setTimeout(()=>{
					const data = {
						"publisher" : publisher,
						"time" : new Date().getFullYear()
					};
					solve(data);
				}, 1000, publisher);

			});
		};

		/*
		getIDs.then((id) => {
			return getRecipe(id);
		})
		.then((data) => {
			return getRelated(data);
		})
		.then((data) => {
			console.log(data)
		})
		.catch((error)=>{
			console.log(error);
		});
		*/

		/*
		async function getRecipesAW () {
			const IDs = await getIDs;
			console.log(IDs);
			const recipe = await getRecipe(IDs);
			console.log(recipe);
			const related = await getRelated(recipe);
			console.log(related);
			return related;
		}*/

		// getRecipesAW();

		/*(async () => {
			const rec = await getRecipesAW();
			console.log("Get the result");
			console.log(rec);
		})();*/

		// getRecipesAW().then((data)=>{console.log(data);});
		
		//fetch("http://localhost:8081/https://www.metaweather.com/api/location/44418/")
		function getWeather(woeid) {
			fetch(`http://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/${woeid}/`)
			.then((data) => {
				console.log(data);
				return data.json();
			})
			.then((result) => {
				const location = result.title;
				const weather_today = result.consolidated_weather[0];
				console.log(`Temperature in ${location} stay between ${weather_today.min_temp} and ${weather_today.max_temp}`);

			})
			.catch((error) => {
				console.log(error);
			});
		}

		async function getWeatherAW (woeid) {
			try {
				const result = await fetch(`http://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/${woeid}/`);
				const data = await result.json();
				const location = await data.title;
					const weather_today = await data.consolidated_weather[0];
				console.log(`AW : Temperature in ${location} stay between ${weather_today.min_temp} and ${weather_today.max_temp}`);
				return data;
			} catch (e) {
				console.log(e);
			}
		}

		let dataLondon;

		getWeatherAW(44418)
		.then((data) => {
			dataLondon = data;
			console.log(dataLondon);
		});


    </script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值