This code doesn't work:
// within an async function
let user, created
if (someCondition) {
[user, created] = await User.findOrCreate({ /* some options */ })
}
console.log(user) // undefined
But this works fine:
// within an async function
let myArray
if (someCondition) {
myArray = await User.findOrCreate({ /* some options */ })
}
console.log(myArray[0]) // the user object, as expected
And this works too:
// within an async function
if (someCondition) {
let [user, created] = await User.findOrCreate({ /* some options */ })
console.log(user) // the user object, as expected
}
I'm declaring user and created outside of the conditional scope because I need those variables at function-level scope. Seems so weird that my first piece of code doesn't work, it's like the destructuring is only working if the let
keyword is used before the array. According to this, it seems like that shouldn't be necessary.