Поток `fetch` не получает вызов, пока поток не закончится - PullRequest
0 голосов
/ 21 апреля 2019

Родителя console.log(r) не вызывают до тех пор, пока не закончится весь запрос ...

fetch("/io/import/anki/progress", {
    method: "POST",
    body: JSON.stringify({
        fileId,
        filename: this.importFile!.name
    }),
    headers: {
        "Content-Type": "application/json; charset=utf-8"
    }
}).then((r) => {
    console.log(r);

    const reader = r.body!.getReader();
    const textDecoder = new TextDecoder();
    let finished = false;

    (async () => {
        while (!finished) {
            const {value, done} = await reader.read();
            if (done) {
                finished = true;
                (this.$refs.uploadModal as any).hide();
            }

            const p = textDecoder.decode(value).trimRight();

            console.log(p);
            if (p) {
                Object.assign(this.progress, JSON.parse(p));
            }
        }
    })();
});

И серверный код, который является Express

public static ankiImportProgress(req: Request, res: Response) {
    const id: string = req.body.fileId;
    const filename: string = req.body.filename;

    res.writeHead(200, {
        "Content-Type": "text/plain",
        "Transfer-Encoding": "chunked",
        "X-Content-Type-Options": "nosniff"
    });

    const anki = new Anki(path.join(tempDir, id, filename), (p: any) => {
        console.log(p),
        res.write(JSON.stringify(p) + "\n");
    });
    anki.export(Config.db!);
    anki.close();

    return res.end();
}

На стороне сервера это console.log() нормально ...

...