gpt4 book ai didi

javascript - 一旦 json stringify writeStream 完成,如何解决 promise ?

转载 作者:行者123 更新时间:2023-12-02 22:58:48 28 4
gpt4 key购买 nike

我使用 npm 的 big-json 来解决大型嵌套对象的 JSON.stringify() 限制。

一旦对象被字符串化,我就会将其保存到文件系统。

然而,这整个过程减慢了我的整体处理速度 - 我假设这与我现在可能正在等待流超时或关闭之前的某些情况有关。

这是我的代码:

const fs = require('fs-extra');
const json = require('big-json');

function offlineSave(objArr){
return new Promise((resolve, reject) => {
let promises = [];
for(let obj of objArr){
const filePath = path.join(__dirname, `${obj}.json`);
//this used to be a promisified fs.writeFile() - which seemed to be faster
promises.push(saveStreamPromise(filePath,obj));
}
Promise.all(promises)
.then((msg) =>{
resolve(`Saved ${msg.length} files`);
});
});
}

let saveStreamPromise = function(filePath, obj){
return new Promise((resolve, reject) =>{
let stream = fs.createWriteStream(filePath, { flag: 'w+' });
const stringifyStream = json.createStringifyStream({ body: obj});
stringifyStream.on('data', strChunk =>{
stream.write(strChunk);
});

//is this the correct way to return a promise upon stringify complete?
stringifyStream.on('end', () =>{
stream.end();
resolve(`${obj} saved!`);
});
});
}

最佳答案

您的代码正在监听读取流的结尾。虽然这可能有效,但最好监听写入流的末尾,这样您只有在数据提交到磁盘后才解决 promise 。

最好将两个流的错误处理添加到任一流拒绝 promise 的任何错误中:

function saveStreamPromise(filePath, obj){
return new Promise((resolve, reject) =>{
let stream = fs.createWriteStream(filePath, { flag: 'w+' });
const stringifyStream = json.createStringifyStream({ body: obj});
stringifyStream.on('data', strChunk =>{
stream.write(strChunk);
});

// resolve when read stream and write stream are done
stringifyStream.on('end', () =>{
stream.end(function() {
resolve('object saved!');
});
}).on('error', reject);
stream.on('error', reject);
});
}

我还建议您使用 .pipe() 自动将一个流提供给另一个流,如下所示:

function saveStreamPromise(filePath, obj){
return new Promise((resolve, reject) =>{
let stream = fs.createWriteStream(filePath, { flag: 'w+' });
const stringifyStream = json.createStringifyStream({ body: obj});
stringifyStream.pipe(stream);

// when using pipe, the write stream will automatically close when the
// readable gets to the end of its data

stream.on('error', reject).on('close', function() {
resolve('object saved!');
});
stringifyStream.on('error', reject);
}

并且,${obj}已保存!只会给您类似“[object object]已保存!”的信息这可能不是您想要的。

关于javascript - 一旦 json stringify writeStream 完成,如何解决 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57863342/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com