Unit 3 Notes (part 2 – Async Calls) from Yakov Fain online Angular training, spring 2016

Promises and Observables

  • Useful for requesting data and other asynchronous uses.
  • Helps avoid ‘callback hell’.

How promises work in standard ES6 spec (not specific to Angular 2)

  • Instantiate instance of ES6 Promise.
  • The instance needs a function with 2 arguments (other signatures may exist):
    • resolve – An instance of a string or other object that you want to pass to a function that will handle a promise that ran successfully.
    • reject – An instance of a string or other object that you want to pass to a function that will handle a promise that does not run successfully.
  • Slide 33 shows an example of how to ‘chain’ together one or more ‘fat arrow’ .then functions followed by a ‘fat arrow’ .catch functions to a promise.
    • The .then receives the resolve value (if successful)
    • The .err receives the reject value (if not successful).

ES6 Generator functions – Provide the calling funciton with some control

  • Can be paused and resumed multiple times
  • Yield the control to the calling script on the same thread
  • When the code runs in to yield; it suspends/pauses
  • Returns an iterator (generator) on invocation
  • To start/continue executing function call next()
  • an asteriks at the end of the keyword function tells ES6 that the function being defined is a generator function:
    • function* doSomething(){
       console.log("Started");
       yield;
       console.log("Resumed");
      }
      
      let iterator = doSomething(); // Runs until yield.
      
      interator.next(); // Proceeds after 'yield'
  • Got to 1:56:45 on video, intro of slide 38
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s