0

I actually sure that there is a simple answer to that , and that I probably just don't use promises correctly but still. Suppose I have a method that returns a promise and I have a method that needs to receive two parameters to work , one that I actually have already. Now since I don't want my functions to be endless I split them to smaller ones , and now I just cant get hold of my parameter. So to be clear , here is something that has not problem working ( might be a bit schematic though)

                var origFeatures = [];
                _.each(wrappedFeatures, function (wrappedFeature) {
                bufferTasks.push(geometryEngineAsync.buffer(
                            [feature.geometry]
                            , feature.geometry.type === "polygon" ? [0] : [5]
                            , esriSRUnit_Meter));
                    }
                }.bind(this));
                all(bufferTasks).then(function(bufferedFeatures){
//doing something with the bufferedFeatures AND origFeatures

}.bind(this))

now Imaging the methods are long so when I split to this

defined somewhere before

                 function _saveGeometries(bufferedFeatures){
                 //do something with buffered features AND the original features
                  }

                    var origFeatures= [];
                _.each(wrappedFeatures, function (wrappedFeature) {
                bufferTasks.push(geometryEngineAsync.buffer(
                            [feature.geometry]
                            , feature.geometry.type === "polygon" ? [0] : [5]
                            , esriSRUnit_Meter));
                    }
                }.bind(this));
                all(bufferTasks)
                .then(this._saveNewGeometries.bind(this))

I of course cannot see the original features because they are not in the scope of the wrapping function

So what I did try to do already is to create a a function in the middle and resolve it with the async result and the additional parameter, but the additional parameter is undefined

                    var featuresFromAttachLayer = [];
                _.each(wrappedFeatures, function (wrappedFeature) {
                bufferTasks.push(geometryEngineAsync.buffer(
                            [feature.geometry]
                            , feature.geometry.type === "polygon" ? [0] : [5]
                            , esriSRUnit_Meter));
                    }
                }.bind(this));
                all(bufferTasks).then(function(bufferedFeatures) {
                    var deff = new Deferred();
                    deff.resolve(bufferedFeatures, wrappedFeatures);
                        return deff.promise;
                    })
                .then(this._saveNewGeometries.bind(this))

Any ideas how to pass the parameter the correct way , both on the good code side and the technical side.

Dan Kuida
  • 1,017
  • 10
  • 18
  • Ok as stupid as it is, but after looking the dojo documentation on the Deferred implementation , it does not support [multiple results from the resolve.][1] [1]: http://dojotoolkit.org/reference-guide/1.10/dojo/Deferred.html SO I endded returning an object wrapping my two parameters. I think it is an ugly way so I would still be happy if someone has a better option – Dan Kuida May 25 '15 at 14:12
  • 1
    possible duplicate of [How do I access previous promise results in a .then() chain?](http://stackoverflow.com/questions/28250680/how-do-i-access-previous-promise-results-in-a-then-chain) – Gabriel L. May 25 '15 at 15:24
  • 1
    @DanKuida this is for the same reason functions can't return multiple values. – Benjamin Gruenbaum May 25 '15 at 17:17

0 Answers0