Skip to content

Instantly share code, notes, and snippets.

@smockle
Created December 18, 2014 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smockle/3f6fa478c82a4b734940 to your computer and use it in GitHub Desktop.
Save smockle/3f6fa478c82a4b734940 to your computer and use it in GitHub Desktop.
Chaining promises in JavaScript.
get("/api/google/sheets/resources")
.then(JSON.parse)
.then(function (context) {
return handlebar("#template", "#output", context);
})
.then(function (context) {
return pourover(context);
})
.then(function (view) {
return linkify(view);
})
.catch(function(err) {
console.error(err);
});
@KidkArolis
Copy link

Wait, why not do:

get("/api/google/sheets/resources")
.then(JSON.parse)
.then(handlebar.bind(null, "#template", "#output"))
.then(pourover)
.then(linkify)
.catch(console.error);

@getify
Copy link

getify commented Jan 13, 2015

@KidkArolis

catch(console.error)

That doesn't work, since error(..) loses its context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment