view src/fetchAndParse.ts @ 95:47d3b5a5bd5e

refactor
author drewp@bigasterisk.com
date Wed, 12 Jan 2022 22:15:13 -0800
parents
children ab7dca42afbd
line wrap: on
line source

import { Store, Parser } from "n3";

export async function fetchAndParse(url: string): Promise<Store> {
    const store = new Store();
    const res = await fetch(url);
    const body = await res.text();
    const parser = new Parser({ format: "N3" });
    await new Promise((done, rej) => {
        parser.parse(body, (err, quad, prefixes) => {
            if (err) {
                throw err;
            }
            if (quad) {
                store.addQuad(quad);
            } else {
                done(null);
            }
        });
    });
    return store;
}