Skip to content

Instantly share code, notes, and snippets.

@RinatMullayanov
Last active August 29, 2015 14:21
Show Gist options
  • Save RinatMullayanov/4ad130fa373689f00484 to your computer and use it in GitHub Desktop.
Save RinatMullayanov/4ad130fa373689f00484 to your computer and use it in GitHub Desktop.

However, promisey code is still hard to read, because promises are basically a bolt-on replacement for language primitives like try, catch, and return:

var db = new PouchDB('mydb');
db.post({}).then(function (result) { // post a new doc
  return db.get(result.id);          // fetch the doc
}).then(function (doc) {
  console.log(doc);                  // log the doc
}).catch(function (err) {
  console.log(err);                  // log any errors
});

What if I told you that, with ES7, you could rewrite the above code to look like this:

let db = new PouchDB('mydb');
try {
  let result = await db.post({});
  let doc = await db.get(result.id);
  console.log(doc);
} catch (err) {
  console.log(err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment