developit / greenlet

🦎 Move an async function into its own thread.
https://npm.im/greenlet
4.67k stars 100 forks source link

Why do I get a return of undefined? #52

Closed ssuess closed 3 years ago

ssuess commented 3 years ago

I am flummoxed by the following, while I can log exactly the result I am looking for, it comes back as undefined in my return

const combo = {}
combo.cprefixes = JSON.parse(JSON.stringify(this.prefixes))
combo.cmyn = this.myn
console.log('combo', combo) // looks good here

const getAnswers = greenlet(async combo => {
  const pkeys = Object.keys(combo.cprefixes)
  const promises = []
  console.log(combo) // looks good here too
  for (const key of pkeys) {
    const url = 'https://my.data.api?myn=' + cmyn + '&prefix=' + combo.cprefixes[key].prefix
    const res = await fetch(url).then(response => response.json())
    promises.push(res)
  }
  console.log('promises:', JSON.parse(JSON.stringify(promises))) // looks good here too
  await Promise.all(promises).then(results => {
    const keys = Object.keys(results)
    console.log('green results: ', results) // looks good here too
    for (const key of keys) {
      combo.cprefixes.find(x => x.prefix === results[key].data.prefix).name = results[key].data.name
    }
    console.log('modified prefixes:', combo.cprefixes) // looks good here too
    return combo.cprefixes
  })
})

await getAnswers(combo)
  .then((response) => {
    console.log('got a response?:', response) // undefined here, why?
  })
nnttoo commented 3 years ago

You seem confused using await and then, the code should be like this

    const combo = {}
    combo.cprefixes = JSON.parse(JSON.stringify(this.prefixes))
    combo.cmyn = this.myn
    console.log('combo', combo) // looks good here

    const getAnswers = greenlet(async combo => {
        const pkeys = Object.keys(combo.cprefixes)
        const promises = []
        console.log(combo) // looks good here too
        for (const key of pkeys) {
            const url = 'https://my.data.api?myn=' + cmyn + '&prefix=' + combo.cprefixes[key].prefix
            const res = await fetch(url).then(response => response.json())
            promises.push(res)
        }
        console.log('promises:', JSON.parse(JSON.stringify(promises))) // looks good here too
        var results = await Promise.all(promises); 

        const keys = Object.keys(results)
        console.log('green results: ', results) // looks good here too
        for (const key of keys) {
        combo.cprefixes.find(x => x.prefix === results[key].data.prefix).name = results[key].data.name
        }
        console.log('modified prefixes:', combo.cprefixes) // looks good here too
        return combo.cprefixes 
    });

    getAnswers(combo)
    .then((response) => {
        console.log('got a response?:', response) // undefined here, why?
    })
ssuess commented 3 years ago

I will try that, thank you!

developit commented 3 years ago

@ssuess you're actually just missing a return statement:

  return await Promise.all(....);