#undefined return from collector

1 messages · Page 1 of 1 (latest)

oblique berry
#

what is temp?
you .then() a promise, you await that promise but don't assign it to a variable

so where do youe expect that value to come from?

#

So it logs yes (line 72) then it logs test for line 75 and then it logs undefined for the console.log of temp

#

@livid snow here, to not have t ping-pong with other conversations

#

what's line 72?

livid snow
livid snow
oblique berry
#

you don't log anything on 72 shibaThinking

livid snow
#

basically i want to assign the outcome of confirmannouncement to a variable

#

so i return something in the function and it doesnt assign

#

Uhh sorry 2 lines down, cus i added the console log for temp

oblique berry
#

again, where do you try to log the response? your code does not align with what you're trying to do, really

livid snow
#

So it logs yes (line 74) then it logs test for line 77 and then it logs undefined for the console.log of temp

oblique berry
#

can you show another screenshot of what you actually have? else this is prone to misunderstandings

livid snow
#

ill remove code that isnt important, this is the essential part

#

theres some stuff inbetween but the essence is this

#

i have a variable i want to assign the return of a function to

#

but it always turns up undefined

oblique berry
#

ah, now i see, you initiatiate temp in the outer scope

#

on 57

livid snow
#

is it conflicting variable names?

#

it shouldnt be right?

oblique berry
#

nah

livid snow
#

Ahh okay i was gonna say xd ive tried so many things now

#

itd essentially be this?

oblique berry
#

yeah, but you don't do that

livid snow
oblique berry
#

well, it'd be more like

const foo = await bar().then(v => return "true")
oblique berry
#

don't define temp in the outer scope and assign it within the then

#

just return it from the then, collect it into a variable and return that from the outer scope

livid snow
#

okay gonna try thanks for your wisdom!

oblique berry
#
async function confirm*() {
  await interaction.edt();
  return interaction.channel.await*().then(v => {
    return "some value";
  })
}
#

that's your situation

#

instead of ```js
async function confirm*() {
let foo = "";
await interaction.edt();
await interaction.channel.await*().then(v => {
foo = "some value"
return foo;
})
}

here you can see why it's undefined
you return foo from the then, but don't do anything with it
you could add a return foo
```js
async function confirm*() {
  let foo = "";
  await interaction.edt();
  await interaction.channel.await*().then(v => {
     foo = "some value"
    return foo; // this does'nt do anything if you don't use the value the promise resolves with
  })
  return foo; // here we return from the confirm* fct
}

but you really shouldn't, and instead just properly handle your promises
and cleanly propagate it through the scopes instead of trying to assign it over promises
(might work for this but is terribly error prone)

livid snow
#

okay!

#

Is there a better way to do it than the .then?

#

just in general?

oblique berry
#

depends on your use case really,
async/await and then are different ways to deal with promises, for sure

livid snow
#

i thought return x easy ill just do that gg but nop xd

oblique berry
#

what you shouldn't do is the empty assignment -> filling it in the then
that leads to confusions and error

#

so your issue atm is that you don't return anything from the outer scope, so the fct will return Promise<undefined> which resolves to undefined if handled

livid snow
#

i have messed up something

#

hm i got it to work

#

somewhat janky