Skip to content

Commit 0561bd9

Browse files
committed
fix: toGenerator should not expand array
1 parent c89476d commit 0561bd9

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ const ERROR_PROMISE_IN_SYNC = '[Quansync] Yielded an unexpected promise in sync
88
function isThenable<T>(value: any): value is Promise<T> {
99
return value && typeof value === 'object' && typeof value.then === 'function'
1010
}
11-
function isGenerator<T>(value: any): value is Generator<T> {
12-
return value && typeof value === 'object' && typeof value[Symbol.iterator] === 'function'
13-
}
1411
function isQuansyncGenerator<T>(value: any): value is QuansyncGenerator<T> {
15-
return isGenerator(value) && '__quansync' in value
12+
return value && typeof value === 'object' && typeof value[Symbol.iterator] === 'function' && '__quansync' in value
1613
}
1714

1815
function fromObject<Return, Args extends any[]>(
@@ -114,7 +111,7 @@ export function quansync<Return, Args extends any[] = []>(
114111
* Converts a promise to a Quansync generator.
115112
*/
116113
export function toGenerator<T>(promise: Promise<T> | QuansyncGenerator<T> | T): QuansyncGenerator<T> {
117-
if (isGenerator(promise))
114+
if (isQuansyncGenerator(promise))
118115
return promise
119116
return fromPromise(promise)()
120117
}

test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ it('yield generator', async () => {
193193
await expect(multiply.async()).resolves.toBe('strstr')
194194
})
195195

196+
it('yield toGenerator array', async () => {
197+
const run = quansync(function* () {
198+
const input = ['1', 2, 3]
199+
const result = yield* toGenerator(input)
200+
expect(result).toBe(input)
201+
return result
202+
})
203+
204+
expect(run.sync()).toEqual(['1', 2, 3])
205+
await expect(run.async()).resolves.toEqual(['1', 2, 3])
206+
})
207+
196208
it('handles tail call', async () => {
197209
const echo = quansync({
198210
sync: (v: string) => v,

0 commit comments

Comments
 (0)