Git Product home page Git Product logo

Comments (2)

Aseelaldallal avatar Aseelaldallal commented on June 11, 2024 1

Thank you Kyle for taking the time to respond! It means a lot, and I got it! :)

from asynquence.

getify avatar getify commented on June 11, 2024

Do you see this part in your first snippet?

function(err, res) {
    G.next(res);
}

What's happening here is that a function callback is in the style of "error-first callback" -- that's what fs.readdir(..) is expecting to receive for its callback -- meaning the first param is reserved for the error signal. In this form, you're basically ignoring/throwing away any error that might come through, and assuming success.

What'd you'd probably want to do is:

function(err, res) {
    if (err) G.throw(err);
    else G.next(res);
}

Here we're splitting the behavior on error vs success, and passing along the appropriate signal (message or exception) to the generator code to handle. This is more robust.

Anyway, either in your version of the snippet, or in mine, you'll notice that the signature maps from 2 inputs (err,res) down to one input (err or res). That's what you're missing in your second snippet.

You pass along the done callback to fs.readdir(..), so of course it's going to receive both err and res, and that's why you're seeing the null in the first argument.

To fix this, you have two options.

Option 1:

function readDirectory(dir) {
    return ASQ(function(done){
        fs.readdir(dir,function(err,...success){
          if (err) done.fail(err);
          else done(...success);
        });
    });
}

See how we're now splitting the behavior and either passing along only the error, or only whatever success messages come through?

But, instead of doing this manually, asynquence provides a convenience helper that does this automatically.

Option 2:

function readDirectory(dir) {
    return ASQ(function(done){
        fs.readdir(dir,done.errfcb);
    });
}

Notice done.errfcb. There's no (..) because we're not calling that function... we're passing along the error-first version of the done(..) callback for fs.readdir(..) to call. done.errfcb is automatically created for you to use in cases where you need an error-first style callback form. :)

Actually, there's an alternate form for option 2 if you prefer.

Option 2b:

function readDirectory(dir) {
    var sq = ASQ();
    fs.readdir( dir, sq.errfcb() );
    return sq;
}

The difference here is that we're constructing the sequence instance (sq), then actually calling errfcb() on the instance to produce the error-first style callback as a sequence step in sq, then passing that callback into fs.readdir(..). Finally, we return that sq sequence so someone else can wait on that step to complete (or error)`.


A separate issue is also that you should be "listening" for any of those signaled exceptions. You can either do that on the sequence instance directly:

ASQ().runner(genProm)
.or(function(err){ .. });

Or, more preferably, you can handle it inside the generator, with a try..catch:

function *genProm() {
    try {
        let exercises = yield readDirectory(dirname);
        console.log(exercises);
    }
    catch(err) {
        // ..
    }
}

Hope that's helpful info for you!

from asynquence.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.