Skip to content

Commit

Permalink
Allow functions to return falsy values (#67796) (#67972)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
crob611 and elasticmachine authored Jun 8, 2020
1 parent 68d9293 commit 8d0d5b7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 8d0d5b7

Please sign in to comment.