Skip to content

Instantly share code, notes, and snippets.

@tobie
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobie/54fbe04b97d0a0b0b621 to your computer and use it in GitHub Desktop.
Save tobie/54fbe04b97d0a0b0b621 to your computer and use it in GitHub Desktop.
ResponsePromise vs. regular promises
function onerror(err) {
// do stuff
}
///////////////// Piping /////////////////
// ResponsePromise
fetch("./contacts.csv").catch(onerror).body.pipe(new CSVParser());
// Regular promise
fetch("./contacts.csv").then(function(response) { response.body.pipe(new CSVParser()); }, onerror);
// Regular promise with getter + invoker
fetch("./contacts.csv").catch(onerror).get("body").invoke("pipe", new CSVParser());
///////////////// Streaming video /////////////////
// ResponsePromise
var video = document.createElement("video");
document.body.appendChild(video);
video.src = fetch("./fr.ogg").body; // How do we handle errors here?
// Regular promise
var video = document.createElement("video");
document.body.appendChild(video); // Is it OK to append a video before setting src?
fetch("./fr.ogg").then(function(response) { video.src = response.body; }, onerror);
// or, if video.src would accept Promise<Response>:
video.src = fetch("./fr.ogg").catch(onerror);
// Regular promise with getter
var video = document.createElement("video");
document.body.appendChild(video);
fetch("./fr.ogg").get("body").then(function(body) { video.src = body; }, onerror);
// or, if video.src would accept promises:
video.src = fetch("./fr.ogg").catch(onerror).get("body");
///////////////// Headers /////////////////
// ResponsePromise
fetch("./contacts.csv").then(function(response) { console.log(response.contentType); }, onerror);
// Regular promise
fetch("./contacts.csv").then(function(response) { console.log(response.contentType); }, onerror);
// Regular promise with getter
fetch("./contacts.csv").get("contentType").then(console.log.bind(console), onerror);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment