0

I have this function which uses promises and is working correctly:

parse: function (fileAsString, options) {
        return when.promise(function(resolve, reject){
            if (fileAsString.length) {
                var metaData = options || {};
                var points = extractPointsFromPCD(fileAsString);
                var cleanedPoints = cleanPoints(points);
                var cordsAndRGBs = createCoordinateAndRGBObjects(cleanedPoints);
                var res = removeExtremePoints(3, cordsAndRGBs);

                var pcdContainer = {
                    coordinateAndRGBObjs: res.points,
                    average: res.average,
                    metaData: metaData,
                    fileString: fileAsString
                };
                // TODO: do meaningful test
                if(res && res.points && res.average && metaData && fileAsString){
                    resolve(pcdContainer);
                } else {
                    // TODO: add actual error
                    reject("something went wrong");
                }
            } else {
                console.log(LOG + "No file!");
            }
        });
    }

As you can see, every function return the value that I need to use in the nextfunction. But I also need to aggregate the various return values because these are the ones the parse function should reuturn. removeExtermePoints even return an object!

I was well on the way to refactor the parse function so that also the subFunctions use promises when I was unsure how to go on. The code I had looked basically like this:

parse: function (fileAsString, options) {
    return when.promise(function(resolve, reject){
       if (fileAsString.length) {
          var metaData = options || {};

        extractPointsFromPCD(fileAsString)
            .then(cleanPoints)
            .then(createCoordinateAndRGBObjects)
            .then(removeExtremePoints)
            .done(function(pcdContainer){
                resolve(pcdContainer);
            });

    } else {
        reject(LOG + "No file to parse!");
    }
});
}

Question 1

I don't have any references to the single return values anymore. I assume I could just use an object that I pass into the first promise, keep pushing it through all the following, adding the values and return that, but I'm not sure if that is a good way of getting to what I had in mind in the first snippet?

Question 2

removeExtremePoints takes a factor as input and I want to stay that way. I assume I can use resolve(3, result) in createCoordinateAndRGBObjects? I really don't like this, because all of a sudden the function createCoordinateAndRGBObjectsdetermines how I want to run removeExtremePoints, I'd rather define the parameter in the parse body.

Also, what if at some point I change something, maybe the promise after createCoordinateAndRGBObjects is not removeExtremepoints for another use case. How would I go about it?

MJB
  • 3,934
  • 10
  • 48
  • 72
  • I'm just curious why you're using promises at all here. I don't see any async operations. This looks like you've way overcomplicated things and this could just be a procedural function that returns a data structure. – jfriend00 Oct 13 '14 at 22:34
  • I want to use promises at other points, and this is just the simplest code segment where I want to test the waters with promises. That being said, I think the code in the second snippet does look cleaner and more comprehensible. – MJB Oct 13 '14 at 22:42
  • If you think using promises when promises are not needed simplifies your code, then you're entitled to your opinion. I have not found that writing synchronous code in an async style is simpler or easier or less error prone - far to the contrary. And, it certainly doesn't perform as well either. Plus your 2nd block of code doesn't actually work either because you aren't keeping track of all the intermediate data anywhere. – jfriend00 Oct 13 '14 at 22:46
  • yeah I know that the second block does not work. As I pointed out in my post, that was what I was refactoring it to when I realized I don't have access to the various return values anymore, as well as no clean idea how to pass the 3 into removeExtremePoints. I'm just using this as an exercise to learn something about how to properly use promises in a case like this. One could just image that all of these function were async and I had 4 nested callbacks – MJB Oct 13 '14 at 22:47
  • And, when you start keeping track of all the various return values, your second block of code will look a lot more like your first block, but with the added complexity of `.then()` calls and asynchronous behavior. Personally, I think you're a bit misguided about what things it is best to use promises for and what it is best to just stick with a traditional function that makes several other function calls. – jfriend00 Oct 13 '14 at 22:48
  • I'm just using this as an exercise to learn something about how to properly use promises in a case like this. One could just image that all of these function were async and I had 4 nested callbacks – MJB Oct 13 '14 at 22:50
  • 1
    I wrote about something similar [here](http://stackoverflow.com/questions/26305267/best-way-to-handle-nested-promises-bluebird/26309488#26309488). It uses [Bluebird](https://github.com/petkaantonov/bluebird) syntax, which I think is better, but it can be done in when.js too using `.all` and `.spread` (`.join` is different in those libraries) – ldmat Oct 14 '14 at 10:00
  • oh nice, is there a way to link that response to my post or should I just delete mine? I think this is what I was looking for. That being said, still quite messy in the end. – MJB Oct 14 '14 at 12:05

0 Answers0