1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| function _fetch(fetch_promise, timeout) { var abort_fn = null;
//这是一个可以被reject的promise var abort_promise = new Promise(function(resolve, reject) { abort_fn = function() { reject('abort promise'); }; });
//这里使用Promise.race,以最快 resolve 或 reject 的结果来传入后续绑定的回调 var abortable_promise = Promise.race([ fetch_promise, abort_promise ]);
setTimeout(function() { abort_fn(); }, timeout);
return abortable_promise; }
//usage: _fetch(fetch('//a.com/b/c'), 2000) .then(function(res) { console.log(res) }, function(err) { console.log(err); });
|