Heim > Fragen und Antworten > Hauptteil
P粉7482188462023-09-06 12:09:35
这应该有效
<html> <body> <h2>JavaScript Promise</h2> <p>Wait 3 seconds (3000 milliseconds) for this page to change.</p> <h1 id="demo"></h1> <script> const myPromise = new Promise(function(myResolve, reject) { setTimeout(() => { reject('I hate you'); }, 3000); }); myPromise.then(function(value) { document.getElementById("demo").innerHTML = value; }).catch(error => { console.log("Catching it"); document.getElementById("demo").innerHTML = error; }); </script> </body> </html>
P粉6968918712023-09-06 10:15:51
myPromise.then(function(value) { document.getElementById("demo").innerHTML = value; }).catch( error => { console.log("Catching it"); document.getElementById("demo").innerHTML = error; });
您需要捕获 .then 之后的错误