0

Hello everyone

My goal:

Get the result out of a Promise that is fullfilled to use the result in a other piece of code

the results that I get are:

Results

but the expected result is the [[PromiseResult]].

I tried other methods I found on here. but there was one problem I did get the result only to show up in the command.log but when I tried to assign it to a value it looked like it skipped right over the code. I know this sounds like a duplicate posts of a post of How to return the response from an asynchronous call? but I tried it and it didn't work for me.

the check boundries returns a Promise

function getData() {
    setLoading(true);
    ref.onSnapshot((querySnapshot) => {
      const items = [];
      querySnapshot.forEach((doc) => {
         let itemurl = 'http://leafletjs.com/examples/custom-icons/leaf-green.png';
        let item = doc.data()
        
        
        console.log(checkBoundaries(doc.data().RawData))
        itemurl =  checkBoundaries(doc.data().RawData)
      console.log( itemurl)
      item.colorIcon = itemurl;
        console.log(item)
        items.push(item);
        console.log(items)

      });
      setRuuviTag(items);
      setLoading(false);
    });
  }

but i cant get item.colorIcon to become the promise result

it would help alot if anyone knew a fix for this

Update

async function checkBoundaries(rdata) {
   let iconurl;
    try {
      const response = await PostRawData(rdata)
      
      const data = response.data
      if (22.00>data.temperature && data.temperature > 4.00) {
        console.log('if')
        iconurl = 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-blue.png'
      }
      else {
        console.log('else')
        iconurl = 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-red.png'
      }
      } catch (err) {
      console.error(err)
      }
      console.log(`this is the url ${iconurl}`)
     
      return iconurl
     
      // ...
    }

I will now get the things i tried but wont work

jaap smit
  • 1
  • 1
  • You need to show us the definition of the `checkBoundaries` which is causing the problem. And yes, it sounds very much like a duplicate, so please explain in detail how you tried the approaches from the answers there. – Bergi May 31 '21 at 02:32
  • 1
    if `checkBoundaries(doc.data().RawData)` returns a Promise, then `itemurl` will, naturally, be a Promise ... use async/await or the standard Promise `.then` method – Jaromanda X May 31 '21 at 02:33
  • You'll want to collect into an array the promises that you get from calling `checkBoundaries` in the `forEach` on the snapshot, and use `Promise.all` – Bergi May 31 '21 at 02:36
  • i think the problem is that its in a firebase function i cant make a sync or something so it wont wait on the outcome – jaap smit May 31 '21 at 02:37
  • See https://stackoverflow.com/q/39151939/1048572 https://stackoverflow.com/q/35879695/1048572 https://stackoverflow.com/q/48432930/1048572 – Bergi May 31 '21 at 02:42

0 Answers0