Reader small image

You're reading from  TypeScript Design Patterns

Product typeBook
Published inAug 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781785280832
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Vilic Vane
Vilic Vane
author image
Vilic Vane

Vilic Vane is a JavaScript engineer with over 8 years of experience in web development. He started following the TypeScript project since it went public, and hes also a contributor of the project. He is now working at Ruff, a startup company building an IoT platform that runs JavaScript on embedded devices.
Read more about Vilic Vane

Right arrow

Promise-based web architecture


To have a better understanding of the differences between Promises and traditional callbacks, consider an asynchronous task like this:

function process(callback) { 
  stepOne((error, resultOne) => { 
    if (error) { 
      callback(error); 
       return; 
      } 
 
  stepTwo(resultOne, (error, resultTwo) => { 
    if (error) { 
      callback(error); 
        return; 
    } 
 
    callback(undefined, resultTwo + 1); 
    }); 
  }); 
} 
 

If we write preceding above in Promise style, it would be as follows:

function process() { 
  return stepOne() 
    .then(result => stepTwo(result)) 
    
.then(result => result + 1); 
}

As in the preceding example, Promise makes it easy and natural to write asynchronous operations with a flat chain instead of nested callbacks. But the most exciting thing about Promise might be the benefits it...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
TypeScript Design Patterns
Published in: Aug 2016Publisher: PacktISBN-13: 9781785280832

Author (1)

author image
Vilic Vane

Vilic Vane is a JavaScript engineer with over 8 years of experience in web development. He started following the TypeScript project since it went public, and hes also a contributor of the project. He is now working at Ruff, a startup company building an IoT platform that runs JavaScript on embedded devices.
Read more about Vilic Vane