Gathering detailed insights and metrics for @byungi/p-cancel-saga
Gathering detailed insights and metrics for @byungi/p-cancel-saga
Gathering detailed insights and metrics for @byungi/p-cancel-saga
Gathering detailed insights and metrics for @byungi/p-cancel-saga
npm install @byungi/p-cancel-saga
Typescript
Module System
Min. Node Version
Node Version
NPM Version
67.9
Supply Chain
89.5
Quality
74.2
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
1,556
Last Day
1
Last Week
4
Last Month
12
Last Year
193
3 Stars
101 Commits
1 Forks
3 Watching
2 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
0.2.1
Package Id
@byungi/p-cancel-saga@0.2.1
Unpacked Size
8.29 kB
Size
2.52 kB
File Count
4
NPM Version
lerna/3.20.1/node@v13.3.0+x64 (win32)
Node Version
13.3.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
33.3%
4
Compared to previous week
Last month
-42.9%
12
Compared to previous month
Last year
-63.7%
193
Compared to previous year
2
1
A cancelable promise with saga.
1import pCancelSaga, { IS_CANCELED } from '@byungi/p-cancel-saga' 2 3const promise = pCancelSaga(function * () { 4 try { 5 const result1 = yield async100msTask() 6 console.log('The 1st task is done.') 7 8 const result2 = yield async100msTask() 9 console.log('The 2nd task is done.') 10 11 return result2 12 } finally { 13 if (yield IS_CANCELED) { 14 console.log('Canceled!') 15 } 16 } 17}) 18 19delay(150).then(() => { 20 promise.cancel() 21 // => "The 1st task is done." 22 // => "Canceled!" 23 // (`The 2nd task is done.` is not print.) 24})
Create a cancelable promise using a saga
(generator).
Similar to p-cancel but without the hassle of checking to cancel after asynchronous task.
1const promise = new PCancel((resolve, reject, onCancel) => { 2 onCancel(() => console.log('Canceled!')) 3 4 async100msTask().then(result1 => { 5 if (!promise.isCanceled) { 6 console.log('The 1st task is done.') 7 8 async100msTask().then(result2 => { 9 if (!promise.isCanceled) { 10 console.log('The 2nd task is done.') 11 resolve(result2) 12 } 13 14 }).catch(reject) 15 } 16 }).catch(reject) 17})
1const promise = pCancelSaga(function * () { 2 try { 3 const result1 = yield async100msTask() 4 console.log('The 1st task is done.') 5 6 const result2 = yield async100msTask() 7 console.log('The 2nd task is done.') 8 9 return result2 10 } finally { 11 if (yield IS_CANCELED) { 12 console.log('Canceled!') 13 } 14 } 15})
1const parentPromise = new PCancel((resolve, reject, onCancel) => { 2 onCancel(() => { 3 console.log('Cancel parent') 4 }) 5 /* ... */ 6}) 7 8const childPromise = pCancelSaga(function * () { 9 try { 10 const result = yield parentPromise 11 /* ... */ 12 } finally { 13 if (yield IS_CANCELED) { 14 console.log('Cancel child') 15 } 16 } 17}) 18 19childPromise.cancel() 20// => Cancel child 21// => Cancel parent
Stop the flow in saga
and throw a CancelError
to promise.
Returns whether the promise is canceled.
Similar to then
but can propagate cancel
to the upper promise.
yield
this symbol in saga
, it returns whether the promise is canceled.
Returns a reusable function using saga
with arguments.
1import { factory, IS_CANCELED } from '@byungi/p-cancel-saga' 2 3const asyncTaskFunction = factory(function * (url, params) { 4 try { 5 const result = yield apiGet(url, params) 6 return result 7 } finally { 8 if (yield IS_CANCELED) { 9 /* ... */ 10 } 11 } 12}) 13 14const promise = asyncTaskFunction('/data', { id: 1 })
If it's just a cancelable task (not a promise), CancelError
is bothersome. This function does not propagate an error.
1import { silent, IS_CANCELED } from '@byungi/p-cancel-saga' 2 3const task = silent(function * () { 4 while (true) { 5 yield delay(1000) 6 yield asyncTask() 7 } 8}) 9 10task.cancel() // => No `unhandledRejection` occurs.
1const promise = pCancelSaga(function * () { 2 const data = yield getDataAsync() // => "data" type is `any` or `unknown`. 3 /* ... */ 4})
In TypeScript, type inference of yield promise in generator functions is difficult. Specify the type or use the provided helper type(AsyncResult<F>
).
1import pCancelSaga, { AsyncResult } from '@byungi/p-cancel-saga' 2 3const promise = pCancelSaga(function * () { 4 const data = (yield getDataAsync()) as {value: string} 5 6 // or 7 const data = (yield getDataAsync()) as AsyncResult<typeof getDataAsync> 8 9 /* ... */ 10})
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
no SAST tool detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More