上次粗翻了一下,感觉没吃透,这次深入体会一下。
<script> function getURL(URL) { return new Promise(function(resolve, reject) { var req = new XMLHttpRequest(); req.open('GET', URL, true); req.onload = function() { if (req.status === 200) { resolve(req.responseText); } else { reject(new Error(req.statusText)); } }; req.onerror = function() { reject(new Error(req.statusText)); }; req.send(); }); } var URL = "http://127.0.0.1:8888/test.html"; getURL(URL).then(function onFulfilled(value) { console.log(value); }).catch(function onRejected(error) { console.error(error); }); Promise.resolve(42).then(function(value) { console.log(value); }); </script>
时间: 2024-10-24 15:02:18