resolve:
new Promise((resolve, reject) = > {
setTimeout(() = > {
resolve({
code: 200})},500)
}).then(res= > {
console.log(res.code)
})
Copy the code
reject:
new Promise((resolve, reject) = > {
setTimeout(() = > {
reject({
code: 400})},500)
}).then(res= > {
console.log(res.code)
}, err= > {
console.log(err.code)
})
Copy the code
catch:
new Promise((resolve, reject) = > {
setTimeout(() = > {
reject({ code: 400})},500)
}).catch((err) = > {
console.log(err.code)
})
Copy the code
Resolve and catch:
new Promise((resolve, reject) = > {
setTimeout(() = > {
if (Math.random() > 0.5) {
resolve({ code: 200})}else {
reject({ code: 400})}},500)
})
.then((res) = > {
console.log(res.code)
})
.catch((err) = > {
console.log(err.code)
})
Copy the code
Throw:
new Promise((resolve, reject) = > {
throw new Error("Error")
}).catch((err) = > {
console.log(err.message)
})
Copy the code