Skip to content

Commit

Permalink
Fix SyncOrAsync util syncValue transfer
Browse files Browse the repository at this point in the history
Our `SyncOrAsync` util is used for cases where a task is >99% of the time
synchronous, yet <1% of the time asynchronous, and where we don't want to
incur any overhead/supplementary complexity of awaiting a Promise which
inherently schedule a microtask when the value is most probably already
there.

It worked well for most usages, but it turns out that a task that starts
as asynchronous would then always lead to the need to rely on Promises,
even once the task is finished (basically, if it started as an "async
value" it could never transform itself to a "sync value").

This does not create any issue but we could gain some minuscule advantage
(well, we could argue that `SyncOrAsync`'s advantage is in itself
minuscule) here by just relying on the value synchronously once the task is
finished.
  • Loading branch information
peaBerberian committed Mar 20, 2024
1 parent e7dfcb8 commit 73af7b5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/utils/sync_or_async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ const SyncOrAsync = {
* @returns {Object}
*/
createAsync<T>(val: Promise<T>): ISyncOrAsyncValue<T> {
let ret = null;
val.then((resolved) => {
let ret: T | null = null;
val.then((resolved: T) => {
ret = resolved;
}, noop);
return {
syncValue: ret,
get syncValue(): T | null {
return ret;
},
getValueAsAsync() {
return val;
},
Expand Down

0 comments on commit 73af7b5

Please sign in to comment.