# Using "if" on Observable

**URL:** <https://talk.observablehq.com/t/using-if-on-observable/5454>\
**Category:** Help\
**Created:** [August 14, 2021, 5:09am UTC](https://talk.observablehq.com/t/using-if-on-observable/5454 "2021-08-14T05:09:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abitrolly](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/abitrolly/32/7214_2.png) [@abitrolly](https://talk.observablehq.com/u/abitrolly)\
**Post date:** [August 14, 2021, 5:09am UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/1 "2021-08-14T05:09:57Z")

</div>

I am trying to do a conditional assignment and it doesn’t work.  
The `datafile` on the screen below should have the value of `attached`,  
which is `FileAttachment {name: "sample.csv"}`, but it is empty?

![image](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/3/387dcb0a61866a9fb3b035e96f0c6aabdf5d138f.png)

> **[Test "if" on Observable](https://observablehq.com/d/7f5b1c7981defcf7)**

---

<div class="post-metadata">

**Author:** ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)\
**Post date:** [August 14, 2021, 8:34am UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/2 "2021-08-14T08:34:43Z")

</div>

your “if” cell puts a dataflow dependency on the variables “uploaded” and “attached”. It updates if these values change.

Because uploaded does not resolve until something is uploaded, the whole cell containing the “if” code is not run at all! That’s what the vertical bar on the left means (pending).

You can peak at a viewof’s value without adding a dataflow dependency on the data like

```auto
    viewof uploaded.value

```

but then when you _do_ upload a file the cell won’t be rerun (it’s no longer reactive).

You can fix this with some manual wiring of addInputListener(‘input’, …) but maybe there is a simpler way to provide a default value to the localInput

```auto
    viewof uploaded = localFileInput({value: attached})

```

now uploaded is initially your sample, which will be overridden if the user uploads a file.

---

<div class="post-metadata">

**Author:** ![abitrolly](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/abitrolly/32/7214_2.png) [@abitrolly](https://talk.observablehq.com/u/abitrolly)\
**Post date:** [August 17, 2021, 7:50am UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/3 "2021-08-17T07:50:03Z")

</div>

Thanks for the explanation. The bar could have a tooltip to explain that it is waiting for resolution of `uploaded`, because attached is already resolved.

I used the `localFileInput({value: attached})` before, but decided to rewrite the logic to be more explicit. The `addInputListener` looks like a JavaScript thing. Is it the official way to write things that may change multiple times?

---

<div class="post-metadata">

**Author:** ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)\
**Post date:** [August 17, 2021, 9:16am UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/4 "2021-08-17T09:16:30Z")

</div>

If you really want to host the if logic in its own cell then you can also do

```auto
viewof uploaded = localFileInput({value: null})

```

which ensures that cell resolves. If either attached, or uploaded changes the if block will be reevaluated automatically by Dataflow.

---

<div class="post-metadata">

**Author:** ![abitrolly](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/abitrolly/32/7214_2.png) [@abitrolly](https://talk.observablehq.com/u/abitrolly)\
**Post date:** [August 17, 2021, 8:58pm UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/5 "2021-08-17T20:58:45Z")

</div>

I confirm this works as needed.

```auto
viewof uploaded = localFileInput({value: null})

```

With that, the following code is reevaluated every time a new file is uploaded.

```auto
datafile = {
  if (uploaded) {
    return uploaded;
  } else {
    return attached;
  }
}

```

But why it is different from the previous definition where it hangs?

```auto
viewof uploaded = localFileInput()

```

---

<div class="post-metadata">

**Author:** ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)\
**Post date:** [August 17, 2021, 9:41pm UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/6 "2021-08-17T21:41:04Z")

</div>

The background to views is here: [Introduction to Views / Observable / Observable](https://observablehq.com/@observablehq/introduction-to-views)

It is not stated there but a view with value ‘undefined’ is not considered resolved.

This also hangs:

```auto
viewof uploaded = localFileInput({value: undefined})

```

As does

```auto
viewof example = html`<div>`

```

I think it is a conscious choice to not resolve anything until an actual value is published. Null is considered a value, whereas undefined is better describing semantically an unset value. data flow is for data, undefined is not data in JS idioms, so it’s a reasonable design choice I think

a good example of the usefulness of holding up data flow until a user does something is [Authentication Simulator / Mike Bostock / Observable](https://observablehq.com/@mbostock/authentication-simulator)

---

<div class="post-metadata">

**Author:** ![radames](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/radames/32/1699_2.png) [@radames](https://talk.observablehq.com/u/radames)\
**Post date:** [August 18, 2021, 12:26am UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/7 "2021-08-18T00:26:59Z")

</div>

That’s very interesting! In regular javascript, you can still test for `undefined` `a === undefined` but it doesn’t seem to work with an unresolved cell on observable. Interesting you can test `viewof value`, but it’s not reactive `viewof file.value === undefined`

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [August 18, 2021, 8:22am UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/8 "2021-08-18T08:22:55Z")

</div>

> [@radames](#):
>
> Interesting you can test `viewof value`, but it’s not reactive `viewof file.value === undefined`

Have a look at [The `.value` is a lie.🎂 (on viewof'ed inputs at least) - #2 by mootari](https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343/2) for an explanation.

---

<div class="post-metadata">

**Author:** ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)\
**Post date:** [November 5, 2021, 7:57pm UTC](https://talk.observablehq.com/t/using-if-on-observable/5454/9 "2021-11-05T19:57:05Z")

</div>

I have hit the difficulty of conditional dataflow a few times myself in other settings, so I sat down and tried to figure out how to do it cleanly even when there is no “value” get out-of-jail-card argument.

> **[How to dataflow merge on Observable](https://observablehq.com/@tomlarkworthy/merge-dataflow)**
>
> In this series, I will explore programming techniques for the notebook platform Observable. Today I am looking at how to merge multiple reactive Dataflow streams into a single stream. This article assumes you are familiar with Observable's non-linear...
