function p(){
return new Promise((resolve, reject) => {
reject();
});
}
p().then(()=>{
console.log(1);
})
I have re-edited the question, just to ask if I don’t write the .catch() of p(), how should I write the browser inside the function p so that it does not report an error
PHP中文网2017-05-19 10:43:01
Two questions:
1.return is not retrun
2.need to add a bracket after new Promise
So the original code is as follows
var x = 10;
function p() {
return new Promise((resolve, reject)=>{
if(1 < x) {
resolve();
}
if(5 < x) {
reject();
}
});
}
p().then(() => {
alert(1)
})
There will be no problem with this call. In addition, after 1<x, we will judge 5<x. The subsequent if is completely unnecessary and will not be executed. The reason is that the state of the promise can only be changed once. The second change will not be effective. .
Even if reject() is executed, the error thrown by promise is a matter of promise and has nothing to do with the P function. The error thrown by promise can only be captured by adding a catch statement after promise. Since the p function returns the promise from new, it is Just add the catch statement after the execution result of p function.
It is recommended to read this article: Promise User Manual