true or false๋ฅผ ํ๋จํ์ธ์.
(1) ๋น๋๊ธฐ ํจ์ ๋ด๋ถ์ ๋น๋๊ธฐ๋ก ๋์ํ๋ ์ฝ๋๋ ๋น๋๊ธฐ ํจ์๊ฐ ์ข
๋ฃ๋ ์ดํ์ ์๋ฃ๋๋ค.
(2) settled ์ํ์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์ํ ์ฑ๊ณต๋๋ฉด resolve ํจ์๋ฅผ ํธ์ถํ์ฌ ํ๋ก๋ฏธ์ค๋ฅผ fulfilled ์ํ๋ก ๋ณ๊ฒฝํ๋ค.
(3) ์๋ฌ ์ฒ๋ฆฌ๋ catch ๋ฉ์๋๋ณด๋ค then ๋ฉ์๋์ ๋ ๋ฒ์งธ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ๊ถ์ฅ๋๋ค.
๋ค์ ์ฝ๋ ์ค, catch ์ฝ๋ ๋ธ๋ก์์ ์๋ฌ ์บ์น๊ฐ ๋์ง ์๋ ์ค๋ช
ํ์ธ์.
try {
setTimeout ( ( ) => {
throw new Error ( "Error!" ) ;
} , 100 ) ;
} catch ( e ) {
// ์๋ฌ ์บ์นX
console . error ( "์บ์นํ ์๋ฌ" , e ) ;
}
๋ค์ ์ฝ๋์ ์ถ๋ ฅ ์์๋ฅผ ์์ฑํ์ธ์.
setTimeout ( ( ) => console . log ( 1 ) , 0 ) ; // (1)
Promise . resolve ( )
. then ( ( ) => console . log ( 2 ) ) // (2)
. then ( ( ) => console . log ( 3 ) ) ; // (3)
๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ฝ๋ฐฑํจ์ ํจํด์ ๋จ์ ์๋ํด ์์ฑํ์ธ์.
(1)error๋ฅผ ์บ์นํ ์ ์๋๋ก ๋ฐ๊ฟ๋ณด์ธ์.
try {
setTimeout ( ( ) => {
throw new Error ( "error" ) ;
} , 1000 ) ;
} catch ( error ) {
console . log ( error ) ;
}
(2) error๊ฐ ์ถ๋ ฅ๋๋ ์์น๋ฅผ ์์ธกํด๋ณด์ธ์.
const f1 = ( ) => {
throw new Error ( "f1 error" ) ;
console . log ( "f1" ) ;
} ;
const f2 = ( ) => {
try {
console . log ( "f2" ) ;
f1 ( ) ;
} catch ( error ) {
console . log ( error ) ; // (2-1)
}
} ;
try {
f2 ( ) ;
} catch ( error ) {
console . log ( error ) ; // (2-2)
}
์ถ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์์ธกํด์ฃผ์ธ์
const promise1 = new Promise ( ( resolve ) =>
setTimeout ( ( ) => {
resolve ( "promise1" ) ;
} , 1000 )
) ;
const promise2 = Promise . resolve ( ( ) => {
setTimeout ( ( ) => {
resolve ( "promise1" ) ;
} , 1000 ) ;
} ) ;
console . log ( promise1 ) ; // (1)
console . log ( promise2 ) ; // (2)
promise1 . then ( ( res ) => console . log ( res ) ) ; // (3)
promise2 . then ( ( res ) => console . log ( res ) ) ; // (4)
๋ค์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์์ ํ๊ณ , ๊ฒฐ๊ณผ์ ์ด์ ๋ฅผ ์ด์ผ๊ธฐํด์ฃผ์ธ์.
try {
setTimeout ( ( ) => { throw new Error ( 'error' ) ; } , 0 ) ;
} catch ( e ) {
console . error ( '์๋ฌ ์บ์น' , e ) ;
}
true or false๋ฅผ ๊ณ ๋ฅด์์ค
(1) then ๋ฉ์๋๋ ๋ ๊ฐ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ธ์๋ก ์ ๋ฌ๋ฐ๊ณ , ๋ง์ฝ ์ฝ๋ฐฑ ํจ์์์ ํ๋ก๋ฏธ์ค๊ฐ ์๋ ๊ฐ์ ๋ฐํํ๋ฉด ์๋ฌต์ ์ผ๋ก ํ๋ก๋ฏธ์ค ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฐํํ๋ค.
(2)ํ๋ก๋ฏธ์ค๋ ์ฝ๋ฐฑ ํจ์์ ๋ฌ๋ฆฌ ์ฝ๋ฐฑ ํจํด์ ์ฌ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์ ์ฝ๋ฐฑ ํฌ์ด ์ผ์ด๋์ง ์๋๋ค.
(3) Promise.all์ Promise.race์ ๋ฌ๋ฆฌ ๋ฉ์๋์ ์ ๋ฌ๋ ํ๋ก๋ฏธ์ค ์ค ํ๋ ์ด์์ด
reject ์ํ๊ฐ ๋๋๋ผ๋ ๋ชจ๋ ์คํ์ด ๋๋์ผ์ง ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํํ๋ค.
๋ค์ ์์ ์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์์ฑํ์์ค
setTimeout ( ( ) => console . log ( 1 ) , 0 ) ;
Promise . resolve ( )
. then ( ( ) => setTimeout ( ( ) => console . log ( 2 ) , 0 ) )
. then ( ( ) => console . log ( 3 ) ) ;
๋ค์ ์ฝ๋๋ฅผ ์คํํ์์ ๋, ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์์ฑํ์์ค.
new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 'abc' ) , 1000 ) )
. then ( res => console . log ( `${ res } then` ) )
. catch ( res => console . log ( `${ res } catch` ) )
. finally ( res => console . log ( `${ res } finally` ) ) ;
๋ค์ ๊ต์ฌ์์ ๋ณด์ฌ์ค ์์์ ํจ๊ป fetch
ํจ์๋ฅผ ์ฌ์ฉํ ๋ ์ฃผ์ํด์ผํ ์ ์ ์์ฑํ์์ค.
const wrongUrl = 'https://jsonplaceholder.typicode.com/XXX/1' ;
fetch ( wrongUrl )
. then ( ( ) => console . log ( 'ok' ) )
. catch ( ( ) => console . log ( 'error' ) ) ;
๊ต์ฌ์์ ๋ช
์ํ ๋ง์ดํฌ๋กํ์คํฌ ํ
์ ๋ํด์ ๋ฌด์์ธ์ง ํ์คํฌ ํ์ ๋น๊ตํ์ฌ ๊ฐ๋จํ ์์ฑํ์์ค.