Sunday 21 July 2019

Use another stream in stream's error handler

Use case: try to get file from the file system. If it doesn't exist fetch it from remote location. Process file and return the result.With promises I can do something like this(simplified pseudocode):function getFile() { return getFileFromFs() .catch(() => { // Ignore error and return another promise // that fetches same file from server return getServerInfo() .then(fetchFileFromServer) }) .then(transformFile); // transform file regardless of it's source } How can I do similar thing with streams?fs.createReadStream('./filePath') .on('error', async () => { // how can i perform async operations and stream // that file from the server here(pipe should still work)? }) .pipe(transformFile)

Submitted July 22, 2019 at 06:13AM by whyyouhavetobemad1

No comments:

Post a Comment