W3C home > Mailing lists > Public > public-media-capture@w3.org > February 2015

Re: Request: Demo of a Javascript Promise?

From: Anne van Kesteren <annevk@annevk.nl>
Date: Thu, 26 Feb 2015 11:02:41 +0100
Message-ID: <CADnb78g8ffOferV5VRvSzfSZpt+odvkh7=oM6CvtBLeC08ymMA@mail.gmail.com>
To: Harald Alvestrand <harald@alvestrand.no>
Cc: "public-media-capture@w3.org" <public-media-capture@w3.org>
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>
 var resolve, reject, p
 p = new Promise(function(rs, rj) {
   resolve = rs
   reject = rj
 p.then(function() { alert("resolve") } , function() { alert("reject") })
<p>Click either button (you can only click once):
<button onclick=resolve()>resolve</button> <button

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

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 16:26:32 UTC