Files
liquidjs/src/util/async.ts
T
499b221f33 docs: remove translations & update homepage (#904)
* docs: add GitHub buttons and improve option docs

* chore: replace husky with prepush check

* docs: revamp homepage and switch to custom GitHub buttons

- Make the docs English-only by removing all zh-cn content, the language switcher UI, and related JS/config

- Rework homepage feature cards (Safe & Typed, Pure JavaScript, Shopify & Jekyll, Streaming) and refresh section colors/layout

- Replace buttons.github.io with custom Star/Sponsor buttons featuring a live star count and dark-mode support

- Drop the buttons.js script and tidy banner, header, footer, and share partials

Co-authored-by: Cursor <[email protected]>

* fix: restore tsconfig settings and changelog build

Re-add suppressImplicitAnyIndexErrors and downlevelIteration removed in
a75033e2c, which broke the rollup TypeScript build on CI. Drop zh-cn
changelog output now that translations were removed.

Co-authored-by: Cursor <[email protected]>

* fix: resolve TS errors without deprecated tsconfig options

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Cursor <[email protected]>
2026-06-07 01:37:51 +08:00

60 lines
1.6 KiB
TypeScript

import { isPromise, isIterator } from './underscore'
export type LiquidAsync<F extends (...args: any[]) => any> =
(sync: boolean, ...args: Parameters<F>) => ReturnType<F> | Promise<ReturnType<F>>
export function toLiquidAsync<F extends (...args: any[]) => any> (
asyncFn: (...args: Parameters<F>) => Promise<ReturnType<F>>,
syncFn?: F
): LiquidAsync<F> {
const syncImpl = syncFn || asyncFn as any
return (sync: boolean, ...args: any[]) => {
return sync ? syncImpl(...args as Parameters<F>) : asyncFn(...args as Parameters<F>)
}
}
// convert an async iterator to a Promise
export async function toPromise<T> (val: Generator<unknown, T, unknown> | Promise<T> | T): Promise<T> {
if (!isIterator(val)) return val
let value: unknown
let done = false
let next: 'next' | 'throw' = 'next'
do {
const state = val[next](value)
done = !!state.done
value = state.value
next = 'next'
try {
if (isIterator(value)) value = toPromise(value)
if (isPromise(value)) value = await value
} catch (err) {
next = 'throw'
value = err
}
} while (!done)
return value as T
}
// convert an async iterator to a value in a synchronous manner
export function toValueSync<T> (val: Generator<unknown, T, unknown> | T): T {
if (!isIterator(val)) return val
let value: any
let done = false
let next: 'next' | 'throw' = 'next'
do {
const state = val[next](value)
done = !!state.done
value = state.value
next = 'next'
if (isIterator(value)) {
try {
value = toValueSync(value)
} catch (err) {
next = 'throw'
value = err
}
}
} while (!done)
return value
}