티스토리 뷰
promise란 (Producing Code)시간 좀 걸리는 코드와 (Consuming Code)반드시 기다려야 하는 코드를 연결하는 객체다.
객체 특징
properties.state (상태) | peding | fulfilled | rejected |
properties.result (결과) | undefined | a result value | an error object |
예제 1. 기본형이다.
const myResolve = () => {console.log("I'm myResolve")}
const myReject = () => {console.log("I'm myReject")}
let myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (시간이 좀 걸린다)
myResolve(); // 성공하면
myReject(); // 에러뜨면
});
// "Consuming Code" (반드시 기다려야 하는 코드)
myPromise.then(
function(value) { /* code if successful */ console.log("done")},
function(error) { /* code if some error */ console.log("fail") }
);
예제 2. 기본형에 조건문을 추가했다.
예제 3. 타이머를 줘서 작동 원리를 본다, myResolve(성공했을 때), myReject(실패했을 때)는 콜백함수다.
참고 링크
JavaScript Promises (w3schools.com)
JavaScript Promises
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
Promise - JavaScript | MDN (mozilla.org)
Promise - JavaScript | MDN
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
developer.mozilla.org
'개발자 > 그냥 js' 카테고리의 다른 글
sort()란 (0) | 2022.01.17 |
---|---|
console.log(`${some}`) vs console.log(some) (0) | 2022.01.16 |
템플릿 리터럴 (0) | 2022.01.12 |
Callback 콜백이란 (1) | 2022.01.12 |
깊은 복사 vs 얕은 복사 (0) | 2022.01.12 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 호이스팅
- postman
- yarn
- 코딩습관
- Console
- arrowfunction
- arrow
- 독커
- Spread
- elasticsearch
- error
- typeorm
- 코드캠프
- GraphQL
- Callback
- 백틱
- Playground
- 도커
- docker
- function
- 콜백
- 에러
- NPM
- axios
- 공부법
- 명령어
- 프로그래머스
- nodemon
- Rest
- js
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
글 보관함