Re: Request: Demo of a Javascript Promise?

On Thu, Feb 26, 2015 at 10:04 AM, Harald Alvestrand
<harald@alvestrand.no> wrote:
> I've tried to experiment a little with Javascript promises, but have
> come to the conclusion that I still don't understand them.
>
> I wonder if someone has a simple demo I can look at?
> What I would want is a Web page that has:
>
> - A function that returns a promise and installs success and failure
> callbacks on it.
> - A button that says "resolve" - when I push it, the promise's success
> function is called, and the promise is resolved.
> - A button that says "cancel" - when I push it, the promise's failure
> function is called, and the promise is rejected.
>
> I tried to write something like that, but my failure to achieve what I
> wanted indicates that there's still something I don't understand.

<!DOCTYPE html>
<script>
 var resolve, reject, p
 p = new Promise(function(rs, rj) {
   resolve = rs
   reject = rj
 })
 p.then(function() { alert("resolve") } , function() { alert("reject") })
</script>
<p>Click either button (you can only click once):
<button onclick=resolve()>resolve</button> <button
onclick=reject()>reject</button>


-- 
https://annevankesteren.nl/

Received on Thursday, 26 February 2015 10:03:05 UTC