I have the code like this:
let generateSmth = function(len, j, stream, headers) {
len--;
j++;
if (len === 0) {
stream.write(headers.map(function(v) {return v + '_' + j;}));
stream.end();
} else {
if (stream.write(headers.map(function(v) {return v + '_' + j;}))) {
generateSmth(len, j, stream, headers);
} else {
stream.once('drain', ()=>{generateSmth(len, j, stream, headers)});
}
}
};
const fs = require('fs'),
myStream = fs.createWriteStream('test.txt');
let myPromise = new Promise((resolve, reject)=>{
try {
generateSmth(10000, 0, ()=>{
resolve(()=>{
});
}, myStream);
} catch (error) {
reject(error);
}
});
I have learned that try/catch is not appropriate way to catch error in generateSmth function. But I can't understand how to catch error if I use streams in my function? Could anyone help me? Thanks.
via Michiko
No comments:
Post a Comment