I have just been thinking the exact same thing over the past couple of 
days. 

If you wanted to keep compatibility with older browsers this could also 
just be a function that takes a value and a node style callback:

Elm.Main.ports.apiSession = function (value, cb) {
  var sess = localStorage.getItem(value);
  if (!sess) {
    cb("NO_SESSION"); // first argument error
  } else {
    cb(null, sess);
  }
}

You could add a guard in the callback so that it can not be called more 
than once. 

I think on the elm side you would need to restrict the errors to values 
that can be sent through ports, and your port definition should probably 
take this into account along with the output type:

port apiSession: String -> Task Decode.Value Decode.Value

Then you can use the task helpers to decode either.

What I really like about this is that you should now be able to write a lot 
of the existing browser API's using ports rather than native code.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to