1

What I want to accomplish:

  1. Make a request from a database.
  2. Combine the results of multiple columns into one array.
  3. Be able to access that array by other functions or methods.

I've accomplished #1 and #2 by making all of the requests to the database in Promise.all() and combining the results into one array.

However, the problem is that I can't access the array in my methods (that I've yet to implement) because the array doesn't contain any content until all the promises have been executed, since it's asynchronous.

Attempts:

1) I tried exporting the array to a different file (in hopes it could extract the array from the promise); however, as you can see below, the only method I found didn't work.

2) The only other solutions I can think of wouldn't work in my situation (like state).

Below is my current progress, I've yet to succeed with requirement number 3. I'm open to any suggestions, even if that means attacking this whole problem a different way then I have.

fileA.js

require('dotenv').config()
const knex = require('knex')

const knexInstance = knex({
  client: 'pg',
  connection: process.env.DB_URL,
})
let shiftR;
const days = ["monday", "tuesday", "wednesday"];   
function dbRequest(day){
    console.log("Day: "+day);
    return knexInstance.select('shift_time',`${day}`).from('shr');

}

module.exports = Promise.all([
            dbRequest(days[0] ? days[0]: null),
            dbRequest(days[1] ? days[1]: null),
            dbRequest(days[2] ? days[2]: null)
])
.then(results => {
    return { shiftR: results } //the array only contains the dates once all the promises
                                //have been completed
});

FileB.js

const shiftRProm = require('./fileA');

shiftRProm.then(({ shiftR }) => {
  console.log(shiftR);
});

I'm stumped on how to fix this. If there's a better way to accomplish what I'm trying to do then I would appreciate the suggestions.

For context, this is what the database ("SHR") looks like, but I don't think this info is needed for the question.

shift_time  monday  tuesday  wednesday (...)
6 AM        1       1        1      
7 AM        2       2        2  
8 AM        3       3        3  
9 AM        3       3        3  
ezg
  • 715
  • 2
  • 7
  • 20
  • 3
    That looks right. Is the `console.log(shiftR);` not logging what you expected? – CertainPerformance Jan 19 '20 at 04:23
  • So, it does. However, because "shiftR" is inside that asynchronous block, I won't be able to access it in any of my methods unless I put my methods in asynchronous blocks as well and I feel like that would be the wrong way to solve this issue. Pretty much, I need to be able to access "shiftR" outside of the asynchronous code so that I can mutate it and view it from anywhere. **I'm referring to methods that I have yet to implement in fileB** – ezg Jan 19 '20 at 04:28
  • 3
    *unless I put my methods in asynchronous blocks as well* That's exactly the right way to do it. Everything that needs to be able to consume the data needs to run asynchronously, because the data is retrieved asynchronously. There's no good way other than using `.then` or `await`. `shiftRProm.then(mainFn)` where you define most of the stuff inside `mainFn` – CertainPerformance Jan 19 '20 at 04:29
  • Ahhh! Ok, I figured there had to be a better way, but that makes sense, thank you for informing me! – ezg Jan 19 '20 at 04:31
  • 1
    If you have lots of files that need to consume something asynchronous and you're having trouble thinking of how to put it all together without writing repetitive code or `.then` everywhere, you can consider [always exporting functions](https://stackoverflow.com/a/52827581) (rather than plain objects or values) - it makes things much easier, since then there's usually only *one* place where you have to call `.then` on a Promise (and then that function can *synchronously* call all the other functions that need to use the resolve value) – CertainPerformance Jan 19 '20 at 04:34
  • Awesome!!! Thank you! I really appreciate your help! – ezg Jan 19 '20 at 04:42

0 Answers0