fetch 是全局量 window 的一个方法, 第一个参数是URL:异步
//url (必须), options (可选) fetch('/some/url', { method: 'get' }).then(function(response) { }).catch(function(err) { // 出错了;等价于 then 的第二个参数,但这样更好用更直观 });
fetch API 也使用了 JavaScript Promises 来处理结果/回调:fetch
// 对响应的简单处理 fetch('/some/url').then(function(response) { }).catch(function(err) { // 出错了;等价于 then 的第二个参数,但这样更直观 }); // 链式处理,将异步变为相似单线程的写法: 高级用法. fetch('/some/url').then(function(response) { return //... 执行成功, 第1步... }).then(function(returnedValue) { // ... 执行成功, 第2步... }).catch(function(err) { // 中途任何地方出错...在此处理 :( });