79502226

Date: 2025-03-11 22:02:22
Score: 0.5
Natty:
Report link

Promisifying surely is a good approach, but it kind of 'kills' the callback technique.

I've created a NodeJS package to deal with callbacks and I want to present it as a possible solution to avoid 'callback hell', without the need to transform everything into Promises.

import { CB } from "callback-utility";
import fs     from "node:fs";


const struct = CB.s ( // 🠄 execute functions in sequence
                 CB.f (fs.mkdir, 'stuff'),
                 CB.f (fs.readFile, 'readMe.txt', 'utf8')
                 CB.f (fs.writeFile, './stuff/writeMe.txt', CB.PREVIOUS_RESULT1) // 🠄 Use result from previous function
               );

// Execute and retrieve results using a single callback function
CB.e (struct, (error, timeout, results) =>
{
    // Check results
    if (timeout || error)
        console.log("Something went wrong while creating the file");
        // get details with result.getErrors();
    else
        console.log("File created");
});

In the above example, all 3 functions with callbacks were invoked and ran in sequence. It could all be executed in parallel or mixing both types (parallel and sequential)

Results from fs.readFile is passed to next function in the row (fs.writeFile).

All results can be retrieved at once, using callback (as above) or a Promise.

Get, please, more info about it in npmjs.com/callback-utility

There are several good options available to address this issue, and I want to present a new one. I hope it will be useful.

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Moacyr Catani