mozilla

Compare Revisions

Promise

Change Revisions

Revision 507433:

Revision 507433 by bfrohs on

Revision 507441:

Revision 507441 by bfrohs on

Title:
Promise
Promise
Slug:
Web/API/Promise
Web/API/Promise
Tags:
"API", "Reference", "DOM Reference", "Experimental", "Référence", "WebAPI", "DOM"
"API", "Reference", "DOM Reference", "Experimental", "Référence", "WebAPI", "DOM"
Content:

Revision 507433
Revision 507441
nn118var promiseCount = 0;
nn120  var thisPromiseCount = ++promiseCount;
121 
n120  log.innerHTML = 'Started<br/>';n123  log.insertAdjacentHTML('beforeend', thisPromiseCount + ') Start
 >ed (<small>Sync code started</small>)<br/>');
t122  p1 = new Promise(                             /* We make a new t125  p1 = new Promise(                   /* We make a new promise: w
>promise: we promise the string 'result' (after waiting 3s) */>e promise the string 'result' (after waiting 3s) */
123              function(resolver) {              /* The init funct126    function(resolver) {              /* The init function is cal
>ion is called with the association PromiseResolver object, pr */>led with the association PromiseResolver object, pr */
127      log.insertAdjacentHTML('beforeend', thisPromiseCount + ') P
 >romise started (<small>Async code started</small>)&lt
 >;br/>');
124                  window.setTimeout(            /* This only is a128      window.setTimeout(              /* This only is an example 
>n example to create asynchronism */>to create asynchronism */
125                      function(resolver) {129        function(resolver) {
126                          resolver('result'); /* We fulfill the p130          resolver(thisPromiseCount); /* We fulfill the promise !
>romise ! */> */
127                      }, 3000, resolver);131        }, Math.random() * 2000 + 1000, resolver);
128              }132    });
129            );133 
130  p1.then(                   /* We defines what to do when the pr134  p1.then(                            /* We defines what to do wh
>omise is fulfilled */>en the promise is fulfilled */
131      function(val) {        /* Just log the message and a value 135    function(val) {                   /* Just log the message and
>*/> a value */
132          log.insertAdjacentHTML('beforeend', 'Promise fulfilled 136      log.insertAdjacentHTML('beforeend', val + ') Promise fulfil
>(' + val +')<br/>')>led (<small>Async code terminated</small>)<br/>
 >');
133      });137    });
134                               138 
135  log.insertAdjacentHTML('beforeend', 'Promise made / Callback se139  log.insertAdjacentHTML('beforeend', thisPromiseCount + ') Promi
>t<br/>');>se made (<small>Sync code terminated</small>)<br/&
 >gt;');
136  log.insertAdjacentHTML('beforeend', 'Sync code terminated<br140}
>/>'); 
137}  

Back to History