0

Right, just so it's out the way, I'm new to JS and Espruino and so got stuck during a project; I'm trying to get the max of an array to be console.logged just after the main function is executed:

const cw = 1;
const ccw = 0;
const dataCW = [];
const peekCW = [];
const dataCCW = [];
const peekCCW = [];
const dataLength = 36;


const getData = (dir, t, fn) => {
    const solarIn = () => analogRead(A5);
    const dataRead = () => ((solarIn() * 65536) * 5.0) / 65536;
    const dataPush = (data) => {
        data.push(dataRead());
    };

    const peekVal = (fnc) => {
        if(dir === 1) {
            for(let i = 0; i < dataLength; i++) {
                setTimeout(() => {
                    dataPush(dataCW);
                    if(dataCW.length === dataLength) {
                        let peekValCW = Math.max.apply(null, dataCW);
                        let peekValPosCW = dataCW.indexOf(peekValCW);
                        peekCW.push(peekValPosCW);
                    }
                }, i * (t / dataLength));
            }
        }
        else if(dir === 0) {
            for(let i = 0; i < dataLength; i++) {
                const forCCW = setTimeout(() => {
                    dataPush(dataCCW);
                    if(dataCCW.length === dataLength) {
                        let peekValCCW = Math.max.apply(null, dataCCW);
                        let peekValPosCCW = dataCCW.indexOf(peekValCCW);
                        peekCCW.push(peekValPosCCW);
                    }
                }, i * (t / dataLength));
            }
        }
        fnc();
    };
    fn(peekVal, peekCW);
};


getData(cw, 1000, (n, m) => n((m) => console.log(m)));

I feel I've spent way too much time trying on my own and only got frustrated with returns like undefined or = [ ] because I can't pass it properly after the function's done executing. Honestly I'm only interested in resolutions involving callbacks, I know Espruino now has Promises implemented and all but feel that this needs to be done with callbacks as I feel this is such an important topic to know/master. Thanks for you help.

Regards, George M.

Progman
  • 16,827
  • 6
  • 33
  • 48
George
  • 23
  • 7
  • 1
    Sounds like you want to call the `fnc();` callback when the last timer has finished, not right after scheduling all the timers. And it seems you even already have code that runs just then… – Bergi Jun 03 '21 at 16:37
  • 1
    Maybe I'm missing something, but shouldn't `fnc()` be `fnc(peekCW)`? – Matt U Jun 03 '21 at 16:38
  • 1
    @MattU …and `fn(peekVal, peekCW)` should be `peekVal(fn)`, or just inlined completely. – Bergi Jun 03 '21 at 16:40
  • @Bergi and Matt - thanks for the comeback! but unfortunately neither of the above seem to work... – George Jun 03 '21 at 21:43
  • @George Can you [edit] the question to show us how you implemented the three bits of advice, please? – Bergi Jun 03 '21 at 21:45
  • @Bergi I've only changed those lines really... they reside in the exact same place and the function called is the same. – George Jun 04 '21 at 08:48
  • @George See my first comment. You need to *move* the `fnc()` line. (And duplicate it, actually). Can you explain why the code uses `if(dataCCW.length === dataLength)`? – Bergi Jun 04 '21 at 12:41
  • @Bergi `if(dataCCW.length === dataLength)` makes sure to pick the maximum value from the array only when all of the 36 vals are in there. Could you post the code in its entirety with your suggestion just so there's no room for interpretation.... I've tried in any other way and still get(at best) an `undefined` or a `=[ ]`. Thanks. – George Jun 04 '21 at 14:10
  • @George "*only when all of the 36 vals are in there.*" - and exactly that is also when you need to call `fnc(peekCW)`. Not right after the loop that started the timeouts. – Bergi Jun 04 '21 at 14:42
  • 1
    @Bergi - yep, have tried that before, but calling it wrong... but after some tweeks made it finally, thanks for the help. – George Jun 04 '21 at 16:09

0 Answers0