0

I'm experimenting with this application using the FHIR.js API and would like to create an iterator for any CarePlans that reference multiple goals; if a person clicks on an arrow the user can cycle through the goals by a click action event. I'm using a generator function and it seems to work, but I'm receiving an undefined value on the first call of my .next method. Can anyone care to explain why this provides the first yield using this function?

  refs = ["#goal-1", "#goal-2", "#goal-3", "#goal-4", "#goal-5"];
  //Goal Id tags referenced for CarePlan

  function* referenceGetter(refs, resource) {
    var i=0;
    var x;
    while (true){
       window.FHIR.oauth2.ready(smart=>{
          smart.api.search({type: resource, query:{_id: refs[i]}})
                        .done(e=>{x = 
                              e.data.entry[0].resource;i++;
                         })
                        .fail(err=>{x= err;i++;});
        });
        //Yield the returned bundles here;
        yield x;
    }
  }

When I run:

getRefs = referenceGetter(refs, "Goal");
getRefs.next()

The first return object is

{value: undefined, done: true}

But then it retrieves the first reference to the first id if I call it again. Any help would be immensely appreciated.

Thank You

Consider:

I'm utilizing the stu3-ver smart-on-fhir api.

akiespenc
  • 305
  • 3
  • 8
  • Don't use generators. Use `async`/`await`. Hint: both will work only when `x` is a promise - which it [clearly isn't](https://stackoverflow.com/q/23667086/1048572) in your code. – Bergi Oct 23 '17 at 13:44
  • Something like this? async function referGetter(refs, resource){ var start = Date.now(); await new Promise((resolve, reject)=>{ FHIR.oauth2.ready(smart=> smart.api.search({type: resource, id: refs[index]}) .done(result=>{resolve(result.data.entry[0].resource);++index}) .fail(err=>{reject(err);++index}) ) }).then(success=>console.log(success)).catch(err=>console.error(err)); var end = Date.now(); console.log("Took approximately", new Date(end-start).getMilliseconds()); } index being a global variable – akiespenc Oct 23 '17 at 15:01
  • How would the generator work though? I feel like the generator is little less taxing with the syntax. – akiespenc Oct 23 '17 at 15:01
  • No, more like `async function referGetter(refs, resource){ var start = Date.now(); var smart = await new Promise(resolve => FHIR.oauth2.ready(resolve)); try { var result = await smart.api.search({type: resource, id: refs[index]}); console.log(result.data.entry[0].resource); } catch(err) { console.error(err); } finally { ++i‌​ndex; } var end = Date.now(); console.log("Took approximately", new Date(end-start).getMilliseconds()); }`. – Bergi Oct 23 '17 at 16:08
  • 1
    No, that's the whole point - `async`/`await` is less taxing with the syntax, it just works. No need to explicitly advance the generator asynchronously. – Bergi Oct 23 '17 at 16:08
  • Thank you, this approach is more concrete – akiespenc Oct 23 '17 at 16:40

0 Answers0