Javascript: Waterfall

JS: Asynchronous programming
Last update: 19 Jan 23:20
0
Students

The async library has a waterfall() function to build chains of asynchronous functions without having to put them into each other. Check the docs to see how it works, and try to solve this exercise using this function.

file.js

Implement and export an asynchronous function unionFiles() that we examined in previous lessons. Here is a common callback implementation:

Examples

import fs from 'fs';

const unionFiles = (inputPath1, inputPath2, outputPath, cb) => {
  fs.readFile(inputPath1, 'utf-8', (error1, data1) => {
    if (error1) {
      cb(error1);
      return;
    }
    fs.readFile(inputPath2, 'utf-8', (error2, data2) => {
      if (error2) {
        cb(error2);
        return;
      }
      fs.writeFile(outputPath, `${data1}${data2}`, (error3) => {
        if (error3) {
          cb(error3);
          return;
        }
        cb(null); // calling it in the end after all error handling
      });
    });
  });
};

Tips

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests