-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added live example of using timeouts; Updated README.hbs.md;
- Loading branch information
1 parent
38320fc
commit 3238d79
Showing
5 changed files
with
198 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Fetch timeout</title> | ||
<script src="../dist/c-promise.umd.js"></script> | ||
</head> | ||
<body> | ||
<p>Make request to endpoint with 10s+ latency</p> | ||
<p>Open your console to see the log</p> | ||
<p>Note that the related request does abort when you undo the promise chain (see the network tab in developer tools)</p> | ||
<button onclick="request(0)">Request url</button> | ||
<button onclick="request(5000)">Request url with timeout 5s</button> | ||
<button onclick="abort()">Abort</button> | ||
<script> | ||
let fetchChain; | ||
const url = "https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=10s"; | ||
let timestamp= Date.now(); | ||
const log= (message, ...values)=> console.log(`[${Date.now()-timestamp}ms] ${message}`, ...values); | ||
const updateTimeStamp= ()=> (timestamp= Date.now()); | ||
|
||
function fetchWithTimeout(url, timeout) { | ||
return new CPromise((resolve, reject, {signal}) => { | ||
fetch(url, {signal}).then(resolve, reject) | ||
}).timeout(timeout) | ||
} | ||
|
||
function request(timeout) { | ||
abort(); | ||
updateTimeStamp(); | ||
log('Fetch started'); | ||
fetchChain = fetchWithTimeout(url, timeout) | ||
.then(response => response.json()) | ||
.then(data => { | ||
log(`Done: `, data); | ||
}, | ||
err => { | ||
log('Error:', err); | ||
}); | ||
} | ||
|
||
function abort() { | ||
fetchChain && fetchChain.cancel(); | ||
} | ||
</script> | ||
</body> | ||
</html> |