Skip to content

Commit 52914f0

Browse files
committed
fix: handle onrejected for thenable
1 parent 7efea90 commit 52914f0

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function fromObject<Return, Args extends any[]>(
5959
}
6060
const fn = (...args: Args): any => {
6161
const iter = generator(...args) as unknown as QuansyncGenerator<Return, Args> & Promise<Return>
62-
iter.then = fn => options.async(...args).then(fn)
62+
iter.then = (...thenArgs) => options.async(...args).then(...thenArgs)
6363
return iter
6464
}
6565
fn.sync = options.sync

test/index.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,19 @@ it('yield promise', async () => {
125125
.toThrowErrorMatchingInlineSnapshot(`[Error: [Quansync] Yielded an unexpected promise in sync context]`)
126126
await expect(run.async('foo')).resolves.toBe('foo')
127127
})
128+
129+
it('handle errors', async () => {
130+
const throwError = quansync({
131+
name: 'throwError',
132+
sync: () => {
133+
throw new Error('sync error')
134+
},
135+
async: async () => {
136+
throw new Error('async error')
137+
},
138+
})
139+
140+
await expect(async () => await throwError()).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: async error]`)
141+
await expect(throwError.async()).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: async error]`)
142+
expect(() => throwError.sync()).toThrowErrorMatchingInlineSnapshot(`[Error: sync error]`)
143+
})

0 commit comments

Comments
 (0)