Skip to content

Instantly share code, notes, and snippets.

@sranso
Last active August 29, 2015 14:17
Show Gist options
  • Save sranso/c888f9d27b299b75faa2 to your computer and use it in GitHub Desktop.
Save sranso/c888f9d27b299b75faa2 to your computer and use it in GitHub Desktop.

#promises talk for queensjs 4/11/15

why do we use them

  • js is single threaded
    • you can add listeners to events, but what if the event happens before you start listening for it?
  • handling errors becomes really difficult in other async options

what are other async options

  • continuation calling style
  • callbacks
  • communicating..?
  • using threads

how do they work

  • finally invoked at end no matter how the promise resolves
  • Promise.all takes an array of promises and creates a promise that fulfills when all of them successfully complete
    • returned same order as passed in

what are some libraries to use

  • native in js es6
  • q
  • bluebird
  • when
  • winjs
  • rsvpjs
  • jquery

what's happening in the future with promises

  • es7 has new features
async function myFunction() {
  let result = await somethingThatReturnsAPromise();
  console.log(result); // cool, we have a result
}
// or
async function myFunction() {
  try {
    await somethingThatReturnsAPromise();
  } catch (err) { 
    console.log(err); // oh noes, we got an error
  }
}
  • can use them via some libs

    • babel
    • regenerator
  • every promise returns a new promise (true?)

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