예시1)
const a = () => {
return new Promise(resolve => {
setTimeout(() => {
console.log(1)
resolve()
}, 1000)
})
}
const b = () => console.log(2)
const wrap = async () => {
await a()
b()
}
wrap()
const delayAdd = index => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(index > 10) {
reject(`${index}는 10보다 클 수 없습니다.`)
return
}
console.log(index)
resolve(index + 1)
}, 1000)
})
}
const wrap = async () => {
try {
const res = await delayAdd(12)
console.log(res)
}
catch (err){
console.error(err)
}
}
wrap()
const getMovies = movieName => {
return new Promise((resolve, reject) => {
fetch(`https://www.omdbapi.com/?apikey=7035c60c&s=${movieName}`)
.then(res => res.json())
.then(json => {
if (json.Response === 'False') {
reject(json.Error)
}
resolve(json)
})
.catch(error => {
reject(error)
})
})
}
let loading = true
getMovies('avengers')
.then(movies => console.log('영화 목록:', movies))
.catch(errpr => console.log('에러 발생', error))
.finally(() => loading = false)