0

I am super new to Gulp so might be on the wrong track here but...

I am trying to create tasks dynamically from values in an object. A simple version of my code is:

var data = {"v1" : "foo",
            "v2" : "bar",
            "v3" : "foobar"};

var taskList = [];

for(var key in data){
    taskList.push(key);
    gulp.task(key, function(){
        console.log(key, data[key]);
        return;
    });
}

gulp.task("master", taskList);

This creates and runs the three tasks as I expected but each one outputs v3 foobar and the values foo and bar are unused. I'm thinking this is maybe some kind of value vs reference issue and each task is receiving key as a reference rather the actual value?

Can anyone shed any light?

Cheers all

popClingwrap
  • 3,919
  • 5
  • 26
  • 44
  • 1
    Not a gulp issue, just an issue with async functions in a for loop. http://stackoverflow.com/questions/13343340/calling-an-asynchronous-function-within-a-for-loop-in-javascript could also be helpful. – tcigrand Sep 17 '15 at 15:45
  • This was indeed the issue and both answers helped to solve it. In the end I actually used the one in the link @AnotherDev posted. Putting the data into an `Array` instead of an `Object` and then using `Array.forEach()` to create the tasks. As always the good folk of stackoverflow deliver the good stuff :) – popClingwrap Sep 17 '15 at 16:07

0 Answers0