Introducción
function doAsyncTask() { var promise = new Promise((resolve, reject) => { setTimeout(() => { console.log("Async Work Complete"); if (error) { reject(); } else { resolve(); } }, 1000); }); return promise; } doAsyncTask().then( (val) => console.log(val), (err) => console.error(err) ); let promise = Promise.resolve('done'); let promise = Promise.reject('fail');
Chaining
Promise.resolve(123) .then((res) => { console.log(res); // 123 return 456; }) .then((res) => { console.log(res); // 456 return Promise.resolve(123); // Notice that we are returning a Promise }) .then((res) => { console.log(res); // 123 : Notice that this `then` is called with the resolved value return 123; }) // Create a rejected promise Promise.reject(new Error('something bad happened')) .then((res) => { console.log(res); // not called return 456; }) .then((res) => { console.log(res); // not called return 123; }) .then((res) => { console.log(res); // not called return 123; }) .catch((err) => { console.log(err.message); // something bad happened });
Más Información
https://basarat.gitbooks.io/typescript/docs/promise.html
https://codecraft.tv/courses/angular/es6-typescript/promises/