Question: I'm finding myself in the following situation. I need to make 3 asynchronous calls: call2 fires only when call1 is done and call3 fires only when call2 is done (I also need to use the deferred object from jQuery).

```
function call1() {
   return $.ajax(...);
}

function call2() {
   return $.ajax(...);
}

function call3() {
   return $.ajax(...);
}

```

Nothing complicated at this point, I could just use the then() function.

```
call1().then(call2).then(call3)

```

The problem is that there are 2 conditions (cond2 and cond3) that will determine whether or not call2 and call3 will be made. If I were to describe it using pseudocode it would look like this:

```
if cond2
  if cond3
    call1().then(call2).then(call3)
  else
    call1().then(call2)
else
  if cond3
    call1().then(call3)
  else
    call1()

```

I know that I can solve my problem by writing a program that uses this structure, but it just doesn't seem right.

My question is, how would you solve this in an efficient way by using the deferred object from jQuery?
Thanks in advance!

Answer: 
By using a type checker, you are opting into the rules that it enforces. Accessing a property on a nullable type is one of those restrictions. So if you want to have exceptions for null values, you need to explicitly throw to prove to Flow that it is what you want. You could for instance make a module like

```
if (!document.body) throw new Error("Unexpectedly missing <body>.");
export const body: HTMLElement = document.body;

export function querySelector(el: HTMLElement, selector: string): HTMLElement {
    const result = el.querySelector(selector);
    if (!result) throw new Error(`Failed to match: ${selector}`);
    return result;
}

```

By throwing, these functions explicitly say "I will return an element" in all cases, and in `null` cases, they will throw exceptions.

Then in your normal code, you are guaranteed you can use those

```
import {body, querySelector} from "./utils";

body.appendChild(document.createElement('div'));

querySelector(body, 'div').setAttribute('thing', 'value');

```

and it will typecheck property.
When I know for sure that my variable won't be null and Flow doesn't, I use an unwrap() function:

```js
export default function unwrap<T>(value: T): $NonMaybeType<T> {
  if (value !== null && value !== undefined) return value
  throw new Error('Unwrapping not possible because the variable is null or undefined!')
}
```