---
# System prepended metadata

title: Untitled

---



|            | Promises | Observables |
| ---------- | -------- | ----------- |
| Ubiquitous |    ✓     |    ✗        |
| Runtime    | Eager    |   Lazy      |
| Dependency | Built-in | External    |
| Can stop   |   ✗      |    ✓        |
| Returns    | Single-value | Many individual values |
| Chaining   |  ✓       | With pipes  |
 
 
<hr>

#### Use case: Get an specific Patient from all the patients I have listed

```
root|
    |-data|
          |-FHIR.Patient|
                        |-Patient1.ttl
                        |-Patient2.ttl 
                        |-Patient3.ttl
                        |- ...
```

Promises:
```typescript=
const getPatientByName = (name: string): Promise<Patient> {
    return client.get(FHIR.Patient).then((sets: DatasetCore[]) => {
        // have to wait for all the datasets to be fetched, potentially from different LDP resources
        for(const set of sets) {
            const patient = new Patient(set);
            if (patient.name === name) {
                return patient;
            }
        }
        return null; // Not found
    });
}


const patient = getPatientByName('jhon doe').then(patient => {
    // do something with patient;
});

```

Observable

```typescript=
/* observable.js */
const getPatientByName = (name: string): Observable<Patient> {
    return client.get(FHIR.Patient).pipe(
        map(set => new Patient(set)),
        filter(patient => patient.name === name),
        take(1), // Only pull values until the one that matches is found
       );
}


getPatientByName('jhon doe').subscribe(patient => {
    // do something with patient
});
```

AsyncIterable

```typescript=
const getPatientByName = async (name: string) Promise<Patient> {
  const  sets = await client.get(FHIR.Patient)
  for await (const set of sets) {
    const patient = new Patient(set)
    if (patient.name === name) return patient // Only pull values until the one that matches is found
  }
  return null // none found
}

const johnDoe = await getPatientByName('john doe')
// do something with patient;


// client.get() needs to be implemented with lazy AsyncGenerator

class Client {
  async getReferencesForShapeTree (shapetree: string):  Promise<string[]>
  async get (shapetree: string): AsyncIterable<T> {
    const iris = await this.getReferencesForShapeTree(shapetree)
    return {
      async * [Symbol.asyncIterator]() {
        for (const iri of iris) {
          yield await this.fetch(iri)
        }
      }
    }
  } 
}
```


<hr>
#### Use case: applying multiple criteria to data selection: retrieve all the patients born after 2000, live in Paris.

Promises - get all the dataset and then iterate over each one selecting the values through `Array.filter`

```typescript=
client.get(FHIR.Patient).then((sets: DatasetCore[]) => {
    const matching = sets.map(set => new Patient(set))
        .filter(patient => patient.birthYear >= 2000)
        .filter(patient => patient.city === 'Paris');
});
```

Observables - as each value is retrieved parse and filter.
```typescript=
client.get(FHIR.Patient).pipe(
    map(set => new Patient(set)),
    filter(patient => patient.birthYear >= 2000),
    filter(patient => patient.city === 'Paris'),
)
```

AsyncIterable - as each value is retrieved parse and filter.
```typescript=
{
  async * [Symbol.asyncIterator]() {
    const sets = await client.get(FHIR.Patient)
    for await (const set of sets) {
      const patient = new Patient(set)
      if (patient.birthYear >= 2000 && patient.city ==='Paris') {
        yield patient
      }
    }
  }
}
```