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)
})()
creator2_ids = creator1_ids? I have a line usingthe_idswhich should be holding the respective objects.the_idsI use a ternary:let the_ids = index === 0 ? creator1_ids : creator2_ids