fetch

sting01·2023년 2월 18일
0

svelte

목록 보기
6/18
<script>
	let list = [];
	const get = async () => {
		const url = 'https://jsonplaceholder.typicode.com/todos/';
		const res = await fetch(url);
		let resData = res.json();

		if (res.ok) {
			return resData;
		} else {
			throw new Error('에러');
		}
	};
</script>

<style>
</style>

<button type="button" on:click="{() => (list = get())}"> 가져오기 </button>

{#await list}
	<p>기다림</p>
{:then values}
	{#each values as { userId, id, title, completed } (id)}
		<li>{id} {title} {completed}</li>
	{/each}
{:catch error}
	<!-- promise was rejected -->
	<p>에러남</p>
{/await}

0개의 댓글