0

I am new to Node and want to call a function and determine whether or not to call the next function based on the results of the first function. The functions are asynchronous so the results are not immediately known. The following code works fine but now I want to add more asynchronous functions and chain them all together.

var doSomething = require('./custom-module1.js'); //returns a boolean
var doAnotherThing = require('./custom-module2.js'); //returns a double

var var1 = process.argv[2];
var var2 = process.argv[3];
var var3 = process.argv[4];

doSomething(var1, var3, function(data) {
    if (data) {
        doAnotherThing(var1,var2, function(data){
            console.log(var1 + ' is value: ' + data);
        });
    }
});

I want to make the code something like this using promises.

getListOfStuff()
.then(
    for(i=0;i<data.length;i++) {
        doSomething(data[i], var3)
        .then(
          if (doSomething returns true) {
            doAnotherThing(data[i], var2)
            .then(doYetAnotherThing(data[i]))
            .then(andEvenMoreThingsToBeDone(data[i]))
          }
        );
    }
);

I read about q and using denodefiy but I can't understand the syntax to make it work. Would it be best to modify my custom modules to return promises so they would be inherently thenable? I get the concept but not the syntax, how do I actually do it?

I tried to denodeify using the following 2 different notations.

var doSomethingDN = Q.denodeify(doSomething);
var doAnotherThingDN= Q.denodeify(doAnotherThing);

doSomethingDN(var1, var3)
.then(
    if(!data) {
        doAnotherThingDN(var1,var2)
    }
);

And this one

var doSomethingDN = Q.nfbind(doSomething);
var doAnotherThingDN= Q.nfbind(doAnotherThing);

doSomethingDN (var1, var3)
.then(
    if(!data) {
        doAnotherThingDN(var1,var2)
    }
);

The if get's an unexpected token error. If I remove then both run but the boolean result from doSomething isn't able to control the program flow.

Jordan
  • 3
  • 4
  • This sounds like you're asking for a general tutorial on adding promises to async code that doesn't use promises. There's a ton written about this already so it would be easier for us to help you if you we understood what specifically you don't understand in that process. Questions here don't work very well if there so open ended that they seem to be asking for a general purpose tutorial. – jfriend00 Oct 01 '16 at 16:14
  • "*Would it be best to modify my custom modules to return promises so they would be inherently thenable?*" - Yes, definitely – Bergi Oct 01 '16 at 17:01
  • Maybe have a look at [How do I convert an existing callback API to promises?](http://stackoverflow.com/q/22519784/1048572) – Bergi Oct 01 '16 at 17:02
  • Please show us your attempt at using `denodefy` so that we can help you with the specific problems you ran into – Bergi Oct 01 '16 at 17:03

1 Answers1

0

You still need to use a callback function with promises:

doSomethingDN (var1, var3)
.then(function(data) {
//    ^^^^^^^^^^^^^^^^
    if (!data) {
        return doAnotherThingDN(var1,var2)
    }
}); /*
^

You were getting a syntax error because you put the function body right as the argument to then.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The if statement doesn't get evaluated. I added a console log before the return and got no output. – Jordan Oct 03 '16 at 13:43
  • I followed your earlier suggestion and changed the custom modules to return promises so it now works like this `doSomething(var1, var3).then(function(response){ if(!response) { doAnotherThing(var1,var2).then(function(data){ return data; }); } });` My apologies I don't know how to format the code to make it easier to view. – Jordan Oct 03 '16 at 14:12