Comment

Comment by jwalton

> My API just returned a single JSON array, so the server couldn’t send anything until it had collected all results.

Why can't you stream a JSON array?

Edit: Here's a (hastily created and untested) node.js example, even:

    class JSONArrayStream extends Transform {
        constructor() {
            super({readableObjectMode: false, writableObjectMode: true});
            this.dataWritten = false;        
        }

        _transform(data, encoding, callback) {
            if (!this.dataWritten) {
                this.dataWritten = true;
                this.push('[\n');
                this.push(JSON.stringify(data) + '\n');
            } else {
                this.push(',' + JSON.stringify(data) + '\n');
            }
            callback();
        }

        _flush(callback) {
            this.push('\n]');
        }
    }
parent jwalton 7 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton