Hành động không đồng bộ đôi khi có thể thất bại. trong trường hợp có lỗi, lời hứa tương ứng sẽ bị từ chối. Chẳng hạn,
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
5 không thành công nếu máy chủ từ xa không khả dụng. Chúng ta có thể sử dụng fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 để xử lý lỗi [từ chối]Chuỗi lời hứa là tuyệt vời ở khía cạnh đó. Khi một lời hứa bị từ chối, điều khiển sẽ chuyển đến trình xử lý từ chối gần nhất trong chuỗi. Điều đó rất thuận tiện trong thực tế
Chẳng hạn, trong đoạn mã bên dưới, URL bị sai [không có trang web nào như vậy] và
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 xử lý lỗifetch['//no-such-server.blabla'] // rejects
.then[response => response.json[]]
.catch[err => alert[err]] // TypeError: failed to fetch [the text may vary]
Hoặc, có thể, mọi thứ đều ổn với trang web, nhưng phản hồi không phải là JSON hợp lệ
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
Cách dễ nhất để bắt tất cả các lỗi là nối thêm
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 vào cuối chuỗifetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
Thông thường,
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 hoàn toàn không kích hoạt, vì không có lỗi. Nhưng nếu bất kỳ lời hứa nào ở trên bị từ chối [sự cố mạng hoặc json không hợp lệ hoặc bất cứ điều gì], thì nó sẽ bắt được nóMã của trình thực thi lời hứa và trình xử lý lời hứa có một "_______60 vô hình" xung quanh nó. Nếu một ngoại lệ xảy ra, nó sẽ bị bắt và coi như một sự từ chối
Ví dụ, mã này
new Promise[[resolve, reject] => {
throw new Error["Whoops!"];
}].catch[alert]; // Error: Whoops!
…Hoạt động chính xác như thế này
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
0"Vô hình
fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
0" xung quanh người thi hành sẽ tự động bắt lỗi và coi đó là lỗi bị từ chốiĐiều này xảy ra không chỉ trong trình thực thi mà cả trong trình xử lý của nó. Nếu chúng tôi
fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
2 bên trong trình xử lý fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
3, điều đó có nghĩa là một lời hứa bị từ chối, do đó, điều khiển sẽ chuyển đến trình xử lý lỗi gần nhấtĐây là một ví dụ
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
4Điều này xảy ra với tất cả các lỗi, không chỉ những lỗi do câu lệnh
fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
2 gây ra. Ví dụ, một lỗi lập trìnhfetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 cuối cùng không chỉ nắm bắt các từ chối rõ ràng mà còn có các lỗi không thường xuyên trong các trình xử lý ở trênNhư chúng ta đã nhận thấy,
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 hoạt động giống như fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
0. Chúng tôi có thể có bao nhiêu trình xử lý fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
3 tùy thích, sau đó sử dụng một fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 duy nhất ở cuối để xử lý lỗi trong tất cả chúngTrong một
fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
0 thông thường, chúng tôi có thể phân tích lỗi và có thể viết lại nếu không xử lý được. Điều tương tự cũng có thể xảy ra với những lời hứaNếu chúng tôi
fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
2 bên trong fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6, thì điều khiển sẽ chuyển đến trình xử lý lỗi gần nhất tiếp theo. Và nếu chúng tôi xử lý lỗi và kết thúc bình thường, thì nó sẽ tiếp tục đến trình xử lý fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
3 thành công gần nhấtTrong ví dụ dưới đây,
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 đã xử lý lỗi thành côngfetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
7Ở đây khối
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 kết thúc bình thường. Vì vậy, trình xử lý fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
3 thành công tiếp theo được gọi làTrong ví dụ dưới đây, chúng ta thấy tình huống khác với
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6. Trình xử lý new Promise[[resolve, reject] => {
throw new Error["Whoops!"];
}].catch[alert]; // Error: Whoops!
8 bắt lỗi và không thể xử lý nó [e. g. nó chỉ biết cách xử lý new Promise[[resolve, reject] => {
throw new Error["Whoops!"];
}].catch[alert]; // Error: Whoops!
9], vì vậy nó lại ném nófetch['//no-such-server.blabla'] // rejects
.then[response => response.json[]]
.catch[err => alert[err]] // TypeError: failed to fetch [the text may vary]
3Sau đó, việc thực hiện nhảy từ
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 new Promise[[resolve, reject] => {
throw new Error["Whoops!"];
}].catch[alert]; // Error: Whoops!
8 đầu tiên sang fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
02 tiếp theo trong chuỗiTrong phần bên dưới, chúng ta sẽ thấy một ví dụ thực tế về việc ném lại
Hãy cải thiện việc xử lý lỗi cho ví dụ tải người dùng
Lời hứa được trả về bằng cách tìm nạp từ chối khi không thể thực hiện yêu cầu. Ví dụ: không có máy chủ từ xa hoặc URL không đúng định dạng. Nhưng nếu máy chủ từ xa phản hồi với lỗi 404 hoặc thậm chí là lỗi 500 thì đó được coi là phản hồi hợp lệ
Nếu máy chủ trả về một trang không phải JSON có lỗi 500 trong dòng
new Promise[[resolve, reject] => {
throw new Error["Whoops!"];
}].catch[alert]; // Error: Whoops!
8 thì sao? fetch['//no-such-server.blabla'] // rejects
.then[response => response.json[]]
.catch[err => alert[err]] // TypeError: failed to fetch [the text may vary]
9Hiện tại, mã cố gắng tải phản hồi dưới dạng JSON bất kể điều gì và chết do lỗi cú pháp. Bạn có thể thấy điều đó bằng cách chạy ví dụ trên, vì tệp
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
05 không tồn tạiĐiều đó không tốt, vì lỗi chỉ rơi vào chuỗi, không có chi tiết. những gì thất bại và ở đâu
Vì vậy, hãy thêm một bước nữa. chúng ta nên kiểm tra thuộc tính
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
06 có trạng thái HTTP không và nếu không phải là 200 thì sẽ báo lỗifetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
0- Chúng tôi tạo một lớp tùy chỉnh cho Lỗi HTTP để phân biệt chúng với các loại lỗi khác. Bên cạnh đó, lớp mới có hàm tạo chấp nhận đối tượng
07 và lưu nó trong lỗi. Vì vậy, mã xử lý lỗi sẽ có thể truy cập phản hồifetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
- Sau đó, chúng tôi kết hợp mã yêu cầu và mã xử lý lỗi thành một hàm tìm nạp
08 và xử lý mọi trạng thái không phải 200 là lỗi. Điều đó thật tiện lợi, bởi vì chúng ta thường cần logic như vậyfetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
- Bây giờ
09 hiển thị một thông báo mô tả hữu ích hơnfetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
Điều tuyệt vời khi có lớp lỗi của riêng chúng ta là chúng ta có thể dễ dàng kiểm tra nó trong mã xử lý lỗi bằng cách sử dụng
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
40Chẳng hạn, chúng tôi có thể đưa ra yêu cầu và sau đó nếu chúng tôi nhận được 404 – hãy yêu cầu người dùng sửa đổi thông tin
Mã bên dưới tải người dùng có tên đã cho từ GitHub. Nếu không có người dùng như vậy, thì nó sẽ hỏi tên chính xác
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
1Xin lưu ý.
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 ở đây bắt hết lỗi, nhưng nó “biết xử lý” chỉ có duy nhất fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
42. Trong trường hợp cụ thể đó, điều đó có nghĩa là không có người dùng nào như vậy và fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6 chỉ thử lại trong trường hợp đóĐối với các lỗi khác, nó không biết điều gì có thể xảy ra. Có lẽ một lỗi lập trình hoặc một cái gì đó. Vì vậy, nó chỉ vẽ lại nó trong dòng
new Promise[[resolve, reject] => {
throw new Error["Whoops!"];
}].catch[alert]; // Error: Whoops!
8Điều gì xảy ra khi một lỗi không được xử lý?
Hoặc chúng ta có thể quên thêm một trình xử lý lỗi vào cuối chuỗi, như ở đây
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
2Trong trường hợp có lỗi, trạng thái lời hứa trở thành "bị từ chối" và quá trình thực thi sẽ chuyển đến trình xử lý từ chối gần nhất. Nhưng không có trình xử lý như vậy trong các ví dụ trên. Vì vậy, lỗi bị "kẹt". Không có mã để xử lý nó
Trong thực tế, giống như lỗi thông thường chưa được xử lý, điều đó có nghĩa là đã xảy ra sự cố nghiêm trọng
Điều gì xảy ra khi một lỗi thông thường xảy ra và không bị bắt bởi
fetch['/article/promise-chaining/user.json']
.then[response => response.json[]]
.then[user => fetch[`//api.github.com/users/${user.name}`]]
.then[response => response.json[]]
.then[githubUser => new Promise[[resolve, reject] => {
let img = document.createElement['img'];
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append[img];
setTimeout[[] => {
img.remove[];
resolve[githubUser];
}, 3000];
}]]
.catch[error => alert[error.message]];
0? . Điều tương tự cũng xảy ra với các từ chối lời hứa chưa được xử lýCông cụ JavaScript theo dõi các từ chối như vậy và tạo ra lỗi toàn cầu trong trường hợp đó. Bạn có thể thấy nó trong bảng điều khiển nếu bạn chạy ví dụ trên
Trong trình duyệt, chúng tôi có thể bắt lỗi như vậy bằng cách sử dụng sự kiện
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
47fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
3Sự kiện là một phần của
Nếu xảy ra lỗi và không có
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
6, trình xử lý fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
47 sẽ kích hoạt và nhận đối tượng fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
60 với thông tin về lỗi, vì vậy chúng tôi có thể thực hiện điều gì đóThông thường, những lỗi như vậy không thể khắc phục được, vì vậy cách tốt nhất của chúng tôi là thông báo cho người dùng về sự cố và có thể báo cáo sự cố cho máy chủ
Trong các môi trường không có trình duyệt như Node. js, có nhiều cách tương tự khác để theo dõi các lỗi chưa được xử lý
6 xử lý các loại từ chối lời hứa. có thể là cuộc gọifetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
62 hoặc lỗi được đưa vào trình xử lýfetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
- Nên đặt chính xác
6 vào những nơi muốn xử lý lỗi và biết cách xử lý. Trình xử lý nên phân tích lỗi [trợ giúp các lớp lỗi tùy chỉnh] và viết lại những lỗi không xác địnhfetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
- Bạn hoàn toàn không nên sử dụng
6, nếu không có cách nào để khắc phục lỗifetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
- Trong mọi trường hợp, chúng ta nên có trình xử lý sự kiện
47 [dành cho trình duyệt và tương tự cho các môi trường khác], để theo dõi các lỗi chưa được xử lý và thông báo cho người dùng [và có thể là máy chủ của chúng ta] về chúng, để ứng dụng của chúng ta không bao giờ “chết”fetch['/'] // fetch works fine now, the server responds with the HTML page .then[response => response.json[]] // rejects: the page is HTML, not a valid json .catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
Và cuối cùng, nếu chúng ta có chỉ báo tải, thì
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
66 là một trình xử lý tuyệt vời để dừng nó khi quá trình tìm nạp hoàn tấtfetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
4Ở đây trên dòng
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
67, chúng tôi biểu thị tải bằng cách làm mờ tài liệu. Phương pháp không thành vấn đề, có thể sử dụng bất kỳ loại chỉ báo nào thay thếKhi lời hứa được giải quyết, có thể là tìm nạp thành công hoặc có lỗi,
fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
68 kích hoạt ở dòng fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
69 và dừng chỉ báoCó một mẹo nhỏ trên trình duyệt
new Promise[[resolve, reject] => {
throw new Error["Whoops!"];
}].catch[alert]; // Error: Whoops!
8 với việc trả lại lời hứa không hết thời gian chờ từ fetch['/'] // fetch works fine now, the server responds with the HTML page
.then[response => response.json[]] // rejects: the page is HTML, not a valid json
.catch[err => alert[err]] // SyntaxError: Unexpected token < in JSON at position 0
68. Đó là bởi vì một số trình duyệt [như Chrome] cần "một chút thời gian" bên ngoài trình xử lý lời hứa để vẽ các thay đổi tài liệu. Vì vậy, nó đảm bảo rằng chỉ báo được dừng trực quan trước khi đi xa hơn trên dây chuyền