2

Calling foo two times creates two subscribers and that is not ok but even so the second call of foo seems to get stuck somewhere because console.log gets called only one time. I know that I should not subscribe in this manner but I really do not see the solution to get myFunc to output the second result

const myFunc = functions.httpsCallable('myFunc')

function foo(inData){
  myFunc({data:inData}).subscribe((res)=>{
    console.log(res)
  })
}

foo('aaaaa')
foo('bbbbb')

second attempt still no luck

myFunc(data){
  return functions.httpsCallable('myFunc')(data)
}
async function foo(inData){
  let res = await lastValueFrom(myFunc({data:inData}))
  console.log(res)
}


foo('aaaaa')
foo('bbbbb')
dsl400
  • 322
  • 3
  • 14

1 Answers1

0

The HttpsCallable function returns a promise by default. As you can see in this related question , there are a few differences between promises and observables, as it seems that you are already working with observables, I would try to convert the promise received from the function to an observable using this method, as suggested in this other question:

import { from } from 'rxjs';
const observable = from(promise);

Although, there are other methods suggested as well in the same question.

I would also like to point you to the Using observables to pass values, I think you might not have shared how you are creating the observer. I'm just guessing but you should also unsubscribe to clean up data ready for the next subscription. Also, missing next notification that is required. As an extra, I would say that if you are not obligated to use observables, handle it as a promise.

Alex
  • 778
  • 1
  • 15
  • 1
    I understand your opinion but look at this: `console.log(httpsCallable('abc'))` outputs `Observable {source: Observable, operator: ƒ}` – dsl400 Dec 04 '21 at 23:39
  • Unfortunately, considering your previous statement “I know I should not subscribe in this manner…”, that is the only issue. In my answer I referenced “Using observables to pass values”. If you follow and correct the issues with your `subscribe()` method, you should be fine. If you receive an observable, you can work it, but with the correct procedure. Can you please give any feedback after following the documentation? – Alex Dec 10 '21 at 19:09
  • In the second example using the await the behavior is the same, I will replace httpsCallable with http request for the moment – dsl400 Dec 11 '21 at 05:48