0

So I have the following code:

async function runQuery(input)
{
  return input
}

async function createAccounts()
{
  return [1, 2]
}



(async function ()
{
  var
    creator1_ids =
        {},
    creator2_ids =
        {}

  let all_accounts =
    await createAccounts(2)

  const tables =
    [
      "list",
      "display_list",
      "map"
    ]

  for (let index in all_accounts)
  {
    let req =
      {
        session:
        {
          account:
            null
        }
      }
    req.session.account =
      all_accounts[index]
    let the_ids =
        index === 0
            ? creator1_ids
            : creator2_ids

    // Create item
    let result =
      await runQuery()

    the_ids.item = 
      1

    for (let table of tables)
    {
      result =
          await runQuery(table)
      the_ids[table] = 
          result
    }
  }
  
  console.log(creator1_ids)
  console.log(creator2_ids)  
})()

Now javascript uses objects as references (so if you assign another variable to an object, changing either variables would change the overall object), however this doesn't seem to be the case here.

creator1_ids remains empty, while only creator2_ids is filled. I'm expecting creator1_ids to be filled similarly.

But this works.

async function runQuery(input)
{
  return input
}

async function createAccounts()
{
  return [1, 2]
}



(async function ()
{

    var
      creator1_ids =
          {},
      creator2_ids =
          {}
    let all_accounts =
        await createAccounts(2)

    const tables =
        [
            "list",
            "display_list",
            "map"
        ]

    async function generate(the_ids, account)
    {
       let req =
        {
          session:
          {
            account:
              null
          }
        }

      // Create item
      let result =
        await runQuery()

      the_ids.item = 
        1

      for (let table of tables)
      {
        result =
            await runQuery(table)
        the_ids[table] = 
            result + account
      }
    }

    await generate(creator1_ids, all_accounts[0])
    await generate(creator2_ids, all_accounts[1])

    console.log(creator1_ids)
    console.log(creator2_ids)
})()

2
  • @Carcigenicate why would I use creator2_ids = creator1_ids? I have a line using the_ids which should be holding the respective objects. Commented Feb 16, 2019 at 19:34
  • @Carcigenicate in the initialisation stage of the_ids I use a ternary: let the_ids = index === 0 ? creator1_ids : creator2_ids Commented Feb 16, 2019 at 19:42

2 Answers 2

3

The conclusion is invalid because the debugging is flawed. Look more closely in your debugger, specifically on this line:

let the_ids =
  index === 0 ?
  creator1_ids :
  creator2_ids

index never equals 0. It does equal '0'. So the === will never be true.

Change the comparison and you'll get the result you are expecting:

let the_ids =
  index == 0 ? // compare values regardless of type
  creator1_ids :
  creator2_ids

Then your loop will first modify creator1_ids, then creator2_ids. Demo:

async function runQuery(input) {
  return input
}

async function createAccounts() {
  return [1, 2]
}



(async function() {
  var creator1_ids = {},
    creator2_ids = {}

  let all_accounts = await createAccounts(2)

  const tables = [
    "list",
    "display_list",
    "map"
  ]

  for (let index in all_accounts) {
    let req = {
      session: {
        account: null
      }
    }
    req.session.account = all_accounts[index]
    let the_ids =
      index == 0 ?
      creator1_ids :
      creator2_ids

    // Create item
    let result = await runQuery()

    the_ids.item = 1

    for (let table of tables) {
      result = await runQuery(table)
      the_ids[table] = result
    }
  }

  console.log(creator1_ids)
  console.log(creator2_ids)
})()

Sign up to request clarification or add additional context in comments.

1 Comment

blah, string, int equality!
2

Index is string so use quote. see below

let the_ids = index === '0'? creator1_ids : creator2_ids

instead of

let the_ids = index === 0  ? creator1_ids: creator2_ids

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.