5

I have a batch job in node.js that: copies files into a directory, does analysis on files, then removes files.

I would like to iterate over an array of jobs and use generators to pause execution until that batch job is complete before starting another job. Here is what I have so far:

const cars = ["toyota", "honda", "acura"];

function copyFilesAndRunAnalysis(car) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() { // simulate some delay
      resolve(); // control should return to generator here
    }, 1000);
  });
}

function* doCar(car) {
  yield copyFilesAndRunAnalysis(car);
}

// BEGIN HERE
console.log('start here');
carBatch = doCar(cars[0]);
carBatch.next(); // confusion here!!!
carBatch.next(); // should this all be in a forEach loop?

What I'd like to do is have a forEach that loops over each car, does all the respective work in the copyFilesAndRunAnalysis method -- pausing until Promise.resolve() and then on to the next one. Trying forEach does not make anything run at all.

Jeff
  • 2,293
  • 4
  • 26
  • 43
  • Not sure that you can use generators to do this. Generators are not designed for handling async operations. What you want do to is covered in the idea of observables. Observables are now in ES7 proposition. – Luka May 21 '16 at 19:59
  • Generators are merely factories for synchronous iterators. You have to combine them either with promises or with observables to handle asynchronous code. Note that `Promise` can emit only a single value/reason while generators are capable of emitting many values over time. More on the subject on [medium](https://medium.com/javascript-scene/the-hidden-power-of-es6-generators-observable-async-flow-control-cfa4c7f31435#.xvyi3o9ir) –  May 22 '16 at 07:39
  • Have a look at [What happens when promise is yielded in javascript?](http://stackoverflow.com/q/33947850/1048572), [ECMA6 generators: yield promise](http://stackoverflow.com/q/30401486/1048572) and [Understanding code flow with yield/generators](http://stackoverflow.com/q/23551418/1048572) to find what you are missing: a driver that runs your generator asynchronously. They don't do that on their own. – Bergi May 23 '16 at 23:48
  • 1
    [Related article](https://medium.com/javascript-scene/the-hidden-power-of-es6-generators-observable-async-flow-control-cfa4c7f31435). – robertklep May 24 '16 at 08:18

2 Answers2

4

You do not use .value at js at Question. The .value of the next() object yielded by Generator would be the Promise returned from copyFilesAndRunAnalysis, where .then() could be chained to .next().value(), Array.prototype.shift() could be used to recursively call doCar until no items remain within original or copy of cars array.

const cars = ["toyota", "honda", "acura"];
let carsCopy = cars.slice(0);

function copyFilesAndRunAnalysis(car) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() { // simulate some delay
      resolve(car); // control should return to generator here
    }, 1000);
  })
}

function* doCar(cars) {
  yield copyFilesAndRunAnalysis(cars);
}

// BEGIN HERE
console.log("start here");
carBatch = doCar(carsCopy.shift());
carBatch.next().value.then(function re(data) {
  console.log(data);
  return carsCopy.length 
         ? doCar(carsCopy.shift()).next().value.then(re) 
         : "complete"
})
.then(function(complete) {
  console.log(complete); 
})

Note, the same process can be achieved utilizing Promise, recursion; without using a Generator function.

const cars = ["toyota", "honda", "acura"];
let carsCopy = cars.slice(0);

function copyFilesAndRunAnalysis(car) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() { // simulate some delay
      resolve(car); // control should return to generator here
    }, 1000);
  })
}

// BEGIN HERE
console.log("start here");
carBatch = copyFilesAndRunAnalysis(carsCopy.shift());
carBatch.then(function re(data) {
  console.log(data);
  return carsCopy.length 
         ? copyFilesAndRunAnalysis(carsCopy.shift()).then(re) 
         : "complete"
})
// do stuff when all items within `cars` have been 
// processed through `copyFilesAndRunAnalysis`
.then(function(complete) {
  console.log(complete); 
})
guest271314
  • 1
  • 15
  • 104
  • 177
1

ES6 generators don't have anything to do with asynchronous execution. They provide usable mechanism for implementing async control flow in third-party code (particularly co).

It may be used like that

co(function* () {
    console.log('start here');

    for (let car of cars) {
        yield copyFilesAndRunAnalysis(car);
    }

    console.log('end here');
});

co transforms wrapped generator function into a promise and doesn't make miracles. All asynchronous actions should be performed inside generator function.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565