teaching machines

CS 330 Lecture 38 – Promises

May 8, 2015 by . Filed under cs330, lectures, spring 2015.

Agenda

TODO

Intentions

Why Doesn’t This Work?

function getEmail(username) {
  var address;
  get('lookup?id=' + username, function onReceipt(usr) {
    address = usr.address;
  });
  return address;
}

var body = '...';
var subject = '...';
var message = new Message(subject, body);

var username = '...';
message.sendTo(getEmail(username));

Code

slow_image.php

<?php
$color = $_REQUEST['color'];
$delay = $_REQUEST['delay'];

sleep($delay);

$path = "$color.png";
$f = fopen($path, 'rb');

header("Content-Type: image/png");
header("Content-Length: " . filesize($path));

fpassthru($f);
fclose($f);
?>

promise.html

<!DOCTYPE html>
<html>
<head>
  <title>...</title>
</head>
<body>

<img src="" id="red"/>
<img src="" id="yellow"/><br/>
<img src="" id="green"/>
<img src="" id="blue"/>

<script>
/*
function loadInSequence() {
  var img = document.querySelector('#red');
  img.onload = function() {
    console.log('red');
    var img = document.querySelector('#yellow');
    img.onload = function() {
      console.log('yellow');
      var img = document.querySelector('#green');
      img.onload = function() {
        console.log('green');
        var img = document.querySelector('#blue');
        img.onload = function() {
          console.log('blue');
          alert('all done!');
        }
        img.src = 'slow_image.php?color=blue&delay=4';
      }
      img.src = 'slow_image.php?color=green&delay=3';
    }
    img.src = 'slow_image.php?color=yellow&delay=2';
  }
  img.src = 'slow_image.php?color=red&delay=1';
}

loadInSequence();
*/

function load(color, delay) {
  return new Promise(function(resolve) {
    var img = document.querySelector('#' + color);
    img.onload = function() {
      console.log(color);
      resolve();
    }
    img.src = 'slow_image.php?color=' + color + '&delay=' + delay;
  });
}

// ----------------------------------------------------------------------------
// Load in sequence with promises
//load('red', 1)
//  .then(function() {return load('yellow', 2);})
//  .then(function() {return load('green', 3);})
//  //.catch(function() {alert('this app broked!');})
//  .then(function() {return load('blue', 4);})
//  .then(function() {return alert('all done');})

// ----------------------------------------------------------------------------
// Load in parallel with promises
var promiseRed = load('red', 1);
var promiseYellow = load('yellow', 2);
var promiseGreen = load('green', 3);
var promiseBlue = load('blue', 4);

// Manually join...
// promiseRed
//  .then(function() {return promiseYellow;})
//  .then(function() {return promiseGreen;})
//  .then(function() {return promiseBlue;})
//  .then(function() {alert('all done!');})
//  .catch(function() {alert("no!!!!")});

// Or use helper...
Promise.all([promiseRed, promiseYellow, promiseGreen, promiseBlue]).then(
  function(result) { alert("okay"); },
  function(error) { alert("uh oh: " + error); }
);
</script>

</body>
</html>

Haiku

on callback hell
Old Phoenix burned up
But he forgot to hit Next
No more phoenixes