UNPKG

dfd

Version:

a minimal deferred-like library.

130 lines (115 loc) 2.92 kB
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Example for Dfd</title> <script src="../dist/dfd.js"></script> <script> window.onload = function() { // dfd dfd() // always function .always(function() { trace('ALWAYS'); }) // done function .done(function() { trace('DONE'); setTimeout(function() { location.reload(); }, 500); }) // error function .fail(function(err) { console.error(err); trace('error function'); setTimeout(function() { location.reload(); }, 500); }) // next function .then(function(cb) { setTimeout(function() { trace('func1'); cb(); }, Math.random() * 500); }) // 1/2 error .then(function(cb) { setTimeout(function() { if (Math.random() < .5) { cb(); } else { cb(new Error('exec error')); } }, Math.random() * 500); }) // next function .then(function(cb) { setTimeout(function() { trace('func2'); cb(); }, Math.random() * 500); }) // parallel .then( function(cb) { setTimeout(function() { trace('parallel func3-1'); cb(); }, Math.random() * 1000); }, function(cb) { setTimeout(function() { trace('parallel func3-2'); cb(); }, Math.random() * 1000); } ) // next function .then(function(cb) { setTimeout(function() { trace('func4'); cb(); }, Math.random() * 500); }) // parallel .then([ function(cb) { setTimeout(function() { trace('parallel func5-1'); cb(); }, Math.random() * 1000); }, function(cb) { setTimeout(function() { trace('parallel func5-2'); cb(); }, Math.random() * 1000); } ]) // wait .wait(500) // next function .then(function(cb) { trace('func6'); cb(); }) // dfd start .resolve(); }; function trace(str) { var elem = document.getElementById('result'), html = elem.innerHTML; if (html != '') html += '<br/>'; html += str.replace(/\n/g, '<br/>'); elem.innerHTML = html; } function clear() { document.getElementById('result').innerHTML = ''; } </script> <style type="text/css"> </style> </head> <body> <h1>Example for Dfd</h1> <p id="result"></p> </body> </html>