※ promise에서의 then
https://velog.io/@tree0787/Promises
// then을 사용한 코드
const getMoviesPromise = () => {
fetch("https://yts.mx/api/v2/list_movies.json")
.then((response) => {
return response.json();
})
.then((potato) => console.log(potato))
.catch((e) => console.log(`✔${e}`));
};
getMoviesPromise ()
// async/await을 사용한 코드
const getMoviesAsync = async () => {
try {
const response = await fetch("https://yts.mx/api/v2/list_movies.json");
const json = await response.json();
console.log(json);
} catch(e){
console.log(`${e}`);
}};
getMoviesAsync()
const getMoviesAsync = async () => {
try {
const response = await fetch("https://yts.mx/api/v2/list_movies.json");
const json = await response.json();
console.log(json);
} catch(e) {
console.log(`${e}`);
} finally {
console.log("we are done!")
}
};
getMoviesAsync()
const getMoviesAsync = async () => {
try {
const [moviesResponse, upcomingResponse] = await Promise.all([
fetch("https://yts.mx/api/v2/list_movies.json"),
fetch("https://yts.mx/api/v2/movie_suggestions.json?movie_id=100"),
]);
const [movies, upcoming] = await Promise.all([
moviesResponse.json(),
upcomingResponse.json(),
]);
console.log(movies, upcoming);
} catch (e) {
console.log(e);
} finally {
console.log("we are done");
}
};
getMoviesAsync();