From 747d620c153cca0cdea10f7e5c04f51e43fa2a7a Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Fri, 11 Sep 2020 23:30:08 +0300 Subject: [PATCH] Updated README.hbs.md; Updated package.json; --- README.md | 60 ++++++++++++++++++------------------------ jsdoc2md/README.hbs.md | 60 ++++++++++++++++++------------------------ package.json | 4 +++ 3 files changed, 56 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 7122cf8..6c569ad 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,32 @@ You may face with a challenge when you need to cancel some long-term asynchronou operation before it will be completed with success or failure, just because the result has lost its relevance to you. +## Live Example + +This is how an abortable fetch ([live example](https://jsfiddle.net/DigitalBrain/c6njyrt9/10/)) with a timeout might look like +````javascript +function fetchWithTimeout(url, options) { + const {timeout, ...fetchOptions}= options; + return new CPromise((resolve, reject, {signal}) => { + fetch(url, {...fetchOptions, signal}).then(resolve, reject) + }, timeout) +} + +const chain= fetchWithTimeout('http://localhost/', {timeout: 5000}) + .then(response => response.json()) + .then(data => console.log(`Done: `, data), err => console.log(`Error: `, err)) + +// setTimeout(()=> chain.cancel(), 1000); +// you able to call cancel() at any time to cancel the entire chain at any stage +// the related network request will also be aborted +```` + +[Live browser example (jsfiddle.net)](https://jsfiddle.net/DigitalBrain/g0dv5L8c/5/) + +[Live nodejs example (runkit.com)](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2) + +Browser playground with fetch + ## How it works The deepest pending CPromise in the chain will be rejected will a `CanceledError`, @@ -43,26 +69,6 @@ If cancellation failed (the chain has been already fulfilled) it will return `fa - static methods `all`, `race` support cancellation and will cancel all other pending promises after they resolved - the `catch` method supports error class filtering -## Live Example - -This is how an abortable fetch ([live example](https://jsfiddle.net/DigitalBrain/c6njyrt9/10/)) with a timeout might look like -````javascript -function fetchWithTimeout(url, timeout) { - return new CPromise((resolve, reject, {signal}) => { - fetch(url, {signal}).then(resolve, reject) - }).timeout(timeout) -} - -const chain= fetchWithTimeout('http://localhost/', 5000); -// chain.cancel(); -```` - -[Live browser example (jsfiddle.net)](https://jsfiddle.net/DigitalBrain/g0dv5L8c/5/) - -[Live nodejs example (jsfiddle.net)](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2) - -Browser playground with fetch - ## Installation :hammer: - Install for node.js using npm/yarn: @@ -104,20 +110,6 @@ CPromise.delay(1000, 'It works!').then(str => console.log('Done', str)); - Run this file using npm run playground or npm run playground:watch command to see the result ## Usage example -A font-end example of wrapping fetch to the CPromise and handling cancellation using signals (AbortController) -````javascript - function cancelableFetch(url) { - return new CPromise((resolve, reject, {signal}) => { - fetch(url, {signal}).then(resolve, reject); - }) - } - // URL with 5 seconds delay to respond - const chain= cancelableFetch('https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s') - .then(console.log, console.warn); - - setTimeout(()=> chain.cancel(), 1000); -```` - Handling cancellation with `onCancel` listeners (see the [live demo](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2)): ````javascript import CPromise from "c-promise"; diff --git a/jsdoc2md/README.hbs.md b/jsdoc2md/README.hbs.md index efb5192..556d5bd 100644 --- a/jsdoc2md/README.hbs.md +++ b/jsdoc2md/README.hbs.md @@ -21,6 +21,32 @@ You may face with a challenge when you need to cancel some long-term asynchronou operation before it will be completed with success or failure, just because the result has lost its relevance to you. +## Live Example + +This is how an abortable fetch ([live example](https://jsfiddle.net/DigitalBrain/c6njyrt9/10/)) with a timeout might look like +````javascript +function fetchWithTimeout(url, options) { + const {timeout, ...fetchOptions}= options; + return new CPromise((resolve, reject, {signal}) => { + fetch(url, {...fetchOptions, signal}).then(resolve, reject) + }, timeout) +} + +const chain= fetchWithTimeout('http://localhost/', {timeout: 5000}) + .then(response => response.json()) + .then(data => console.log(`Done: `, data), err => console.log(`Error: `, err)) + +// setTimeout(()=> chain.cancel(), 1000); +// you able to call cancel() at any time to cancel the entire chain at any stage +// the related network request will also be aborted +```` + +[Live browser example (jsfiddle.net)](https://jsfiddle.net/DigitalBrain/g0dv5L8c/5/) + +[Live nodejs example (runkit.com)](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2) + +Browser playground with fetch + ## How it works The deepest pending CPromise in the chain will be rejected will a `CanceledError`, @@ -43,26 +69,6 @@ If cancellation failed (the chain has been already fulfilled) it will return `fa - static methods `all`, `race` support cancellation and will cancel all other pending promises after they resolved - the `catch` method supports error class filtering -## Live Example - -This is how an abortable fetch ([live example](https://jsfiddle.net/DigitalBrain/c6njyrt9/10/)) with a timeout might look like -````javascript -function fetchWithTimeout(url, timeout) { - return new CPromise((resolve, reject, {signal}) => { - fetch(url, {signal}).then(resolve, reject) - }).timeout(timeout) -} - -const chain= fetchWithTimeout('http://localhost/', 5000); -// chain.cancel(); -```` - -[Live browser example (jsfiddle.net)](https://jsfiddle.net/DigitalBrain/g0dv5L8c/5/) - -[Live nodejs example (jsfiddle.net)](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2) - -Browser playground with fetch - ## Installation :hammer: - Install for node.js using npm/yarn: @@ -104,20 +110,6 @@ CPromise.delay(1000, 'It works!').then(str => console.log('Done', str)); - Run this file using npm run playground or npm run playground:watch command to see the result ## Usage example -A font-end example of wrapping fetch to the CPromise and handling cancellation using signals (AbortController) -````javascript - function cancelableFetch(url) { - return new CPromise((resolve, reject, {signal}) => { - fetch(url, {signal}).then(resolve, reject); - }) - } - // URL with 5 seconds delay to respond - const chain= cancelableFetch('https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s') - .then(console.log, console.warn); - - setTimeout(()=> chain.cancel(), 1000); -```` - Handling cancellation with `onCancel` listeners (see the [live demo](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2)): ````javascript import CPromise from "c-promise"; diff --git a/package.json b/package.json index 8e7cea9..27c6f92 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,10 @@ "docs": "jsdoc2md -t jsdoc2md/README.hbs.md lib/c-promise.js > README.md", "docs:namepaths": "jsdoc2md ./lib/c-promise.js --namepaths" }, + "repository": "https://github.com/DigitalBrainJS/c-promise.git", + "bugs": { + "url": "https://github.com/DigitalBrainJS/c-promise/issues" + }, "nyc": { "lines": 60, "functions": 60,