3

I've just started using generators in my JS and I've got my head around bits of it but I'm confused of how to return data from them. In my generator function (runGenerators) below I'm successfully making three async calls and getting the returned data, however I don't know how I return the final data (aUpdatedTagsCollection) back from the generator function.

Here's my generator:

ImageData.prototype.runGenerators = function* runGenerators() {
 let aImages, aTagsCollection, aUpdatedTagsCollection;
 aImages = yield this.getImagesFromFolder();
 aTagsCollection = yield this.getImageData(aImages);
 aUpdatedTagsCollection = yield this.findCountry(aTagsCollection);
 console.log(aUpdatedTagsCollection); //This prints the correct result, but how do I return it
};

Each of the methods (getImagesFromFolder, getImageData & findCountry) call the this.oIterator.next(data); next iterator when they are done, sending data to the next method.

This is my findCountry method:

ImageData.prototype.findCountry = function findCountry(aTagsCollection) {
 let oSelf = this,
 j = 0;
 for (var i = 0; i < aTagsCollection.length; i++) {
 geocoder.reverse({
 lat: aTagsCollection[i].GPSLatitude.description,
 lon: aTagsCollection[i].GPSLongitude.description
 }, function (oError, oResult) {
 if (oResult !== undefined && oResult[0] !== undefined) {
 aTagsCollection[j].country = oResult[0].country;
 aTagsCollection[j].countryCode = oResult[0].countryCode;
 if ((j + 1) === aTagsCollection.length) {
 oSelf.oIterator.next(aTagsCollection);
 }
 }
 j++;
 });
 }
}

This is the method which calls the generator function

ImageData.prototype.retrieveImageData = function retrieveImageData() {
 this.oIterator = this.runGenerators();
 this.oIterator.next();
};

And, finally this is the method which instantiates the ImageData class and invokes the retrieveImageData method

let oImageData = new ImageData();
let retrievedData = oImageData.retrieveImageData();
console.log(retrievedData); //undefined (obviously) because isn't returning anything but what/how do I return???

Any help would be great appreciated - I hope I've explained myself correctly.

asked Feb 21, 2016 at 1:20

1 Answer 1

1

You should be able to use one last yield aUpdatedTagsCollection; or finally { yield aUpdatedTagsCollection;} or return aUpdatedTagsCollection; in the runGenerators function and make a last

var receivedData = oImageData.oIterator.next();

call from your oImageData object.

answered Mar 19, 2016 at 15:38
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I've since learned you cannot return data from a generator function. I did solve this problem by passing in a callback function to the generator, then invoking the callback from the generator, passing back the aUpdatedTagsCollection.
@AndyMeek, what does that mean? yield stops execution and returns value, just like return operator, but with ability to continue from where it left. yield parameter acts as return value of generator.next() call, while argument of generator.next() call acts as return value for yield statement. If you don't use generator's functionality at all, why even bother with them?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.