Allow functions to return falsy values (#67796)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
52c518a6ae
commit
ce7940adc2
|
@ -303,6 +303,47 @@ describe('createStreamingBatchedFunction()', () => {
|
|||
expect(await promise3).toEqual({ foo: 'bar 2' });
|
||||
});
|
||||
|
||||
test('resolves falsy results', async () => {
|
||||
const { fetchStreaming, stream } = setup();
|
||||
const fn = createStreamingBatchedFunction({
|
||||
url: '/test',
|
||||
fetchStreaming,
|
||||
maxItemAge: 5,
|
||||
flushOnMaxItems: 3,
|
||||
});
|
||||
|
||||
const promise1 = fn({ a: '1' });
|
||||
const promise2 = fn({ b: '2' });
|
||||
const promise3 = fn({ c: '3' });
|
||||
await new Promise((r) => setTimeout(r, 6));
|
||||
|
||||
stream.next(
|
||||
JSON.stringify({
|
||||
id: 0,
|
||||
result: false,
|
||||
}) + '\n'
|
||||
);
|
||||
stream.next(
|
||||
JSON.stringify({
|
||||
id: 1,
|
||||
result: 0,
|
||||
}) + '\n'
|
||||
);
|
||||
stream.next(
|
||||
JSON.stringify({
|
||||
id: 2,
|
||||
result: '',
|
||||
}) + '\n'
|
||||
);
|
||||
|
||||
expect(await isPending(promise1)).toBe(false);
|
||||
expect(await isPending(promise2)).toBe(false);
|
||||
expect(await isPending(promise3)).toBe(false);
|
||||
expect(await promise1).toEqual(false);
|
||||
expect(await promise2).toEqual(0);
|
||||
expect(await promise3).toEqual('');
|
||||
});
|
||||
|
||||
test('rejects promise on error response', async () => {
|
||||
const { fetchStreaming, stream } = setup();
|
||||
const fn = createStreamingBatchedFunction({
|
||||
|
|
|
@ -106,7 +106,7 @@ export const createStreamingBatchedFunction = <Payload, Result extends object>(
|
|||
if (response.error) {
|
||||
responsesReceived++;
|
||||
items[response.id].future.reject(response.error);
|
||||
} else if (response.result) {
|
||||
} else if (response.result !== undefined) {
|
||||
responsesReceived++;
|
||||
items[response.id].future.resolve(response.result);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue