-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@async-library/core #2
Comments
What exactly are you targeting here? Internal API? External API? Saga-like effect yields with return values? Just flow control? Just out of my head:
function* correct() {
const promise = Promise.resolve(3);
const result: number = yield promise;
}
function* problematic() {
const promise = Promise.resolve(3);
const result: string = yield promise; // this is wrong and cannot be checked by TS
} So I'm not a big fan for generators that yield/inject more than exactly one fixed type (as that is possible with TS). As for the point that currently, promises might leak memory and lead to uncaught promise rejections, we could cicumvent that:
So those promises (that would in the case of async-library/react-async#92 (comment) ) cause an uncaught error. Would just either resolve to |
Agreed, I have a preference for promises over generators as well. However, will it allow us to implement subscriptions? I've updated the issue to add a draft of the core API. It's by no means complete. |
Not really, but a generator would make for a weird subscription, too. You'd have too loop over the Iterable or call next() manually. I'd rather use a callback for that. |
@async-library/core
will contain all the core functionality that Async Library provides. It will generally not be used directly, but be a dependency of@async-library/react-async-hook
for example. It should have feature parity withreact-async
andreact-async-hook
(outside of React specifics).Features
To discuss:
Core API
States:
initial
nothing happened yet (orreinitialize
was invoked)pending
promise is loading or pausedfulfilled
promise was fulfilled with datarejected
promise was rejected with errorConfig options:
fn
async function that will be invoked onrun
runOnInit
flag to enable running on init (mount) [do we really need this?]initialValue
initialize value to a predefined value or error [should this be separate values for data and error?]Callback options:
onInit
when first initialized (i.e. mount)onData
when promise has fulfilled with dataonError
when promise has rejected with erroronAbort
when promise has abortedonDestroy
when destroying the instance (i.e. unmount)Actions:
run
invokefn
abort
cancel a pending promise and invokeAbortController::abort
reinitialize
abort and reset state toinitial
Metadata:
runCount
number of timesrun
was invoked (automatically or manually)startedAt
when the lastrun
was invokedfinishedAt
when the last promise was fulfilled or rejectedThis API deviates quite a bit from React Async. Notably:
promiseFn
anddeferFn
are combined intofn
promise
is dropped as an option. You can just pass() => promise
instead.The text was updated successfully, but these errors were encountered: