# Value sent on outgoing port not received on first render

**URL:** <https://discourse.elm-lang.org/t/value-sent-on-outgoing-port-not-received-on-first-render/3931>\
**Category:** Learn\
**Created:** [July 4, 2019, 1:05am UTC](https://discourse.elm-lang.org/t/value-sent-on-outgoing-port-not-received-on-first-render/3931 "2019-07-04T01:05:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![curiouslearn](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@curiouslearn](https://discourse.elm-lang.org/u/curiouslearn)\
**Post date:** [July 4, 2019, 1:05am UTC](https://discourse.elm-lang.org/t/value-sent-on-outgoing-port-not-received-on-first-render/3931/1 "2019-07-04T01:05:13Z")

</div>

Today, I posted a question [about strange behavior related to input value updating](https://discourse.elm-lang.org/t/weird-behavior-updating-value-of-one-input-also-updates-value-of-another-input/3929). Before someone suggests a solution, I am trying to do a workaround. For this I thought that I will control this input completely from JS. To do so, however, I would prefer to send the initial value from Elm. So I wrote the following port code:

```
   port startTsPort : JE.Value -> Cmd msg

   app.ports.startTsPort.subscribe(function(val) {
        console.log("from startTsPort: ", val);
    })

```

When I first do a fresh load of the page (no cache) nothing is logged to the console. However, if I just refresh the page again, then the value `startTsPort` is logged as expected.

My question: How do I ensure that the value is received on the JS side on the first load?

---

<div class="post-metadata">

**Author:** ![curiouslearn](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@curiouslearn](https://discourse.elm-lang.org/u/curiouslearn)\
**Post date:** [July 4, 2019, 3:16pm UTC](https://discourse.elm-lang.org/t/value-sent-on-outgoing-port-not-received-on-first-render/3931/2 "2019-07-04T15:16:17Z")

</div>

On further trials, I figured that this is likely due to the fact that when Elm sends the value to JS, the JS is not yet ready. I think this might be the case because if I insert a wait (using Process.sleep) and then send the value, it is logged to the console on the initial fresh load.

Updated question: Is there a DOMContentLoaded on Elm, so that I can send the value after the DOM is loaded and ready to go? Using an arbitrary `sleep` does not seem like a good solution?

---

<div class="post-metadata">

**Author:** ![Libbum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/libbum/32/1311_2.png) [@Libbum](https://discourse.elm-lang.org/u/Libbum)\
**Post date:** [July 5, 2019, 9:40am UTC](https://discourse.elm-lang.org/t/value-sent-on-outgoing-port-not-received-on-first-render/3931/3 "2019-07-05T09:40:12Z")

</div>

> [@curiouslearn](#):
>
> Is there a DOMContentLoaded on Elm, so that I can send the value after the DOM is loaded and ready to go?

Not officially AFAIK. Could you give me a rework of your code with this new implementation?

If I understand what you’re saying I think its a similar problem to what I’ve come across already.  
Take a read of this thread:

> [@requestAnimationFrame problem when using --optimize](https://discourse.elm-lang.org/t/requestanimationframe-problem-when-using-optimize/2885):
>
> I’m doing some stuff with D3 and sending updates through ports. One port is having issues when I compile with --optimise—it works fine in debug mode however. I need D3 to select the #map div and do work on it, but this is rendered in my View, so I use requestAnimationFrame to make sure the View is rendered before starting the D3 commands: port drawMap : () -\> Cmd msg app.ports.drawMap.subscribe(function() { requestAnimationFrame(function() { drawMap(); }); return null;…

There’s currently no nice solution to this one either. I’ve kept the [wait for element](https://github.com/Libbum/Odyssey/blob/1e641075658b00ad293862b81265f6bfe16910ea/src/init.js#L280-L289) implementation in my solution. So that’s not too different from an arbitrary sleep.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [July 15, 2019, 9:40am UTC](https://discourse.elm-lang.org/t/value-sent-on-outgoing-port-not-received-on-first-render/3931/4 "2019-07-15T09:40:14Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
