Promise의 then,catch,finally 요약
요약: 1、Promise의 상태는 바뀌면 다시 바꿀 수 없다.const promise = new Promise((resolve, reject) => {
resolve("success1");
reject("error");
resolve("success2");
});
promise
.then(res => {
console.log("then: ", res);
}).catch(err => {
console.log("catch: ", err);
})
// then:success1
2. then 및 catch 모두 새로운 Promise 로 돌아갑니다.3、catch는 어디에 연결되든지 상부에서 포착하지 못한 오류를 포착할 수 있다.const promise = new Promise((resolve, reject) => {
reject("error");
resolve("success2");
});
promise
.then(res => {
console.log("then1: ", res);
}).then(res => {
console.log("then2: ", res);
}).catch(err => {
console.log("catch: ", err);
}).then(res => {
console.log("then3: ", res);
})
//"catch: " "error"
//"then3: " undefined
4. Promise에서 임의의 비promise로 되돌아오는 값은 promise 대상으로 감싸집니다. 예를 들어 return 2 는 return Promise.resolve(2) 로 포장됩니다.5、Promise의.then또는.catch는 여러 번 호출될 수 있지만 Promise 내부의 상태가 바뀌고 값이 하나 생기면 후속적으로 호출.then또는.catch할 때마다 직접 이 값을 받게 된다.const promise = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('timer')
resolve('success')
}, 1000)
})
const start = Date.now();
promise.then(res => {
console.log(res, Date.now() - start)
})
promise.then(res => {
console.log(res, Date.now() - start)
})
//'timer'
//'success' 1001
//'success' 1002
// Promise .then .catch , Promise 。
// promise , , .then .catch 。
6、.then 또는.catch 중return 하나error의 대상은 오류를 던지지 않기 때문에 후속.catch에 잡히지 않는다.Promise.resolve().then(() => {
return new Error('error!!!')
}).then(res => {
console.log("then: ", res)
}).catch(err => {
console.log("catch: ", err)
})
//"then: " "Error: error!!!"
// 4 6 , promise promise ,
// return new Error('error!!!') return Promise.resolve(new Error('error!!!'))。
물론 당신이 실수를 한다면 아래의 임의의 것을 사용할 수 있다.return Promise.reject(new Error('error!!!'));
// or
throw new Error('error!!!')
7、.then 또는 .catch가 되돌아오는 값은promise 자체가 될 수 없습니다. 그렇지 않으면 사순환을 일으킬 수 있습니다.8、.then 또는.catch의 매개 변수는 함수이고 비함수에 들어가면 값 투과가 발생할 수 있기를 기대한다.Promise.resolve(1)
.then(2)
.then(Promise.resolve(3))
.then(console.log)
// 1
// then then , ,
// , resolve(1) then 。
9、.then 방법은 두 개의 매개 변수를 수신할 수 있다. 첫 번째는 처리에 성공한 함수, 두 번째는 처리에 실패한 함수, 그리고 어떤 때는 catch두 번째 매개 변수의 간편한 쓰기라고 생각할 수 있다..then10、 방법도 하나.finally로 되돌아간다. 그는 Promise가 끝날 때 결과Promise든 resolved든 안에 있는 리셋 함수를 실행한다.rejected① 방법은 .finally() 대상의 마지막 상태에 상관없이 실행된다Promise② 방법의 리셋 함수는 어떠한 매개 변수도 받아들이지 않는다. 즉, .finally() 함수에서 .finally()의 최종 상태가 Promise인지 resolved인지 알 수 없다는 것이다.
③ 마지막에 반환되는 기본값은 이전 Promise 객체 값이지만 예외가 발생하면 예외 rejected 객체를 반환합니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
const promise = new Promise((resolve, reject) => {
resolve("success1");
reject("error");
resolve("success2");
});
promise
.then(res => {
console.log("then: ", res);
}).catch(err => {
console.log("catch: ", err);
})
// then:success1const promise = new Promise((resolve, reject) => {
reject("error");
resolve("success2");
});
promise
.then(res => {
console.log("then1: ", res);
}).then(res => {
console.log("then2: ", res);
}).catch(err => {
console.log("catch: ", err);
}).then(res => {
console.log("then3: ", res);
})
//"catch: " "error"
//"then3: " undefined
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('timer')
resolve('success')
}, 1000)
})
const start = Date.now();
promise.then(res => {
console.log(res, Date.now() - start)
})
promise.then(res => {
console.log(res, Date.now() - start)
})
//'timer'
//'success' 1001
//'success' 1002
// Promise .then .catch , Promise 。
// promise , , .then .catch 。
Promise.resolve().then(() => {
return new Error('error!!!')
}).then(res => {
console.log("then: ", res)
}).catch(err => {
console.log("catch: ", err)
})
//"then: " "Error: error!!!"
// 4 6 , promise promise ,
// return new Error('error!!!') return Promise.resolve(new Error('error!!!'))。
return Promise.reject(new Error('error!!!'));
// or
throw new Error('error!!!')
Promise.resolve(1)
.then(2)
.then(Promise.resolve(3))
.then(console.log)
// 1
// then then , ,
// , resolve(1) then 。이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.