0

the below code uses a when sequence to fire off a sequence of the same promise but passing in slightly different data.

when/sequence accepts a single common set of arguments for the entire sequence the same going to each. So I "tricked" it by closing on the index that instance/element will need inside a function calling the promise and then had that function grab the index needed for that promise when the function is called for that element.

This all works I am just wondering if there is some package method out there, or I can modify my pTasker or otherwise deal with different inputs to a set of promises in a more robust/general way.

I thought maybe make an array of "copies" of the input argument and then the enclosed function can grab the appropriate one from the array although that doesn't change much the enclosed function still needs to know which iterate it is. Any experts out there have a better/best way to skin this cat, or is this as good as it gets?

  let cmds = args.components.map( i => {
        return args => {
            let cmpt = i-1;
            args.component = args.components[cmpt];
            Debug.L1('cmpt, component',cmpt, args.component)
            return device.cmd(args)
        }
       });

return _.pTasker(cmds, args)
    .then(cmds => {
        console.log('all responses ', cmds.map(cmd => {
            return cmd.response
        }))
        resolve()
    })
    .catch(function(e) {
        console.log('error: ', e)
        reject(e)
    })

and the promise sequence builder

const fs = require('fs'),
    sequence = require('when/sequence'),
    parallel = require('when/parallel'),
    node = require('when/node');

// Lift fs.readFile so it returns promises
let readFile = node.lift(fs.readFile);

let self = module.exports = {

    // Run an array of "tasks" in sequence as promises, embedded array will be treated as running in parallel
    pTasker: function(tasks, data) {

        return sequence(
            tasks.map(function(item) {
                if (Array.isArray(item)) {
                    return data => parallel(item, data)
                } else {
                    return item
                }
            }), data)
    },
DKebler
  • 1,216
  • 1
  • 15
  • 27
  • Sounds like you're actually looking for [`reduce`](https://github.com/cujojs/when/blob/master/docs/api.md#whenreduce) when you want to loop over an array sequentially? – Bergi Oct 23 '16 at 21:06
  • yes but I can't get the syntax right. Their example isn't really helping me. I don't understand why the required extra function "canvas" before "src". I don't have one. https://github.com/cujojs/when/wiki/Examples#whenreduce. Should I start a new post or edit this one cause now I have a new question really? As I have it no exceptions but there is no promise running... – DKebler Oct 25 '16 at 20:57
  • I think my problem is that I am passing an object, then by a promise adding a key to it and resolving it back. If I understand https://github.com/cujojs/when/wiki/Examples#whenreduce then first is what is returned and second is what is passed. This does nothing, nothing fires no debug. I know this works cause if call processor with cmds[0] it works. return reduce(cmds, function(processor,cmd) { Debug.L1('running processor for component ', cmd.component) return processor(cmd) }) – DKebler Oct 25 '16 at 21:11
  • @Bergi, I made a new post for my new problem here. http://stackoverflow.com/questions/40250132/need-correct-call-to-when-reduce – DKebler Oct 25 '16 at 21:36

0 Answers0