async/await

odini·2019년 3월 11일
0

Javascript

목록 보기
3/3
const getData = () => {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
            if (Math.floor(Math.random()*2)){
				resolve('1');
            } else {
				reject({isError: true, code: 700, message: 'message'});
            }			
        },300);
    })
};

Promise

getData()
.then((response) => {
	console.log('## Promise SUCCEED :', response);
})
.catch((e) => {
	console.error('## Promise FAILED :', e);
});

Async/Await

const run = async () => {
	const response = await getData();
	console.log(response);
};
run();

Error Handling

const run = async () => {
    try {
        const response = await getData();
        console.log('## async await SUCCEED :', response);
    } catch(e) {
        console.error('## async await FAILED :', e);
    }
};
run();

0개의 댓글