TypeScript Asynchronous - Tuong-Nguyen/Angular-D3-Cometd GitHub Wiki

Callback function

Promise

  • Required: target = ES2015
  • Simple API: then catch
  • May be chained
  • Created by passing a function to Promise constructor
// Define an async work
function doAsyncWork(resolve, reject){
   // perform async calls
   if (success) resolve(data);
   else reject(reason);
}

// Create a promise for async work
let p: Promise<string> = new Promise<string>(doAsyncWork);

// Handle promise result - THEN - CATCH
p.then(stringData => console.log(stringData))
 .catch(reason => console.log(reason));

// Handle promise - THEN: both success and error callback are used.
p.then(stringData => console.log(stringData), reason => console.log(reason))
⚠️ **GitHub.com Fallback** ⚠️