如何在打字稿中返回数组的承诺

时间:2019-04-17 21:35:34

标签: node.js typescript promise

我正在尝试以promise的形式返回数组。我首先解压缩文件,然后使用csv-parse解析文件。我将所有返回的对象保存在一个数组中,然后返回该数组

我试图在没有答应的情况下返回,而译员没有抱怨。但我想扩大规模

/**
 * Wraps writeFile in a promise.
 * @param content The Base64 content of the file to read.
 * @returns A buffer containing the contents of the file.
 */
protected writeFileAsync(path: string, content: Buffer): Promise<IProblem[]> {
    new Promise<Buffer>((resolve, reject) => {
        fs.writeFile(path, content,  "base64", (err) => {
            if (err) {
                reject(err);
            }

            resolve(content);
        });
    })
    .then(result => {
        fs.readFile(result, (err, data) => {
            if (err) {
                Log.error("Error while reading the zip file");
                this.listOfProblems = [];
                return this.listOfProblems;
            }
            let zip: JSZip = new JSZip();
            zip.loadAsync(data)
            .then(contents => {
                Object.keys(contents.files).forEach(filename => {
                    zip.file(filename).async('nodebuffer')
                    .then(content => {
                        let parser: csvParse.Parser = csvParse((data, err) => {
                            if (err) {
                                Log.error("Error while reading the zip file");
                                this.listOfProblems = [];
                                return this.listOfProblems;
                            }
                            let problem: IProblem;

                        }) as csvParse.Parser;
                        fs.createReadStream(content).pipe(parser);
                    })
                })
            })
        })
    })
    .catch(() => {
        Log.error("Error while reading the zip file");
        this.listOfProblems = [];
        return this.listOfProblems;
    });
    return this.listOfProblems;
}

最后一行显示错误

1 个答案:

答案 0 :(得分:0)

您必须返回诺言而不是清单。

return new Promise<Buffer>((resolve, reject) => {