/** * EXAMPLE1: 在 Promise 的 callback 中回傳另一個 Promise **/ const returnPromise = new Promise((resolve, reject) => { // 立即執行 console.log('In first Promise, start callback') setTimeout(() => { let data = 10 resolve(data) console.log('In fist Promise, resovle data') }, 1500) }) // 在 Promise 的 Callback 中回傳另一個 Promise .then((value) => { return new Promise((resolve, reject) => { setTimeout(() => { resolve(value + 3) console.log('In second Promise, resovle data') }, 1500) }) }) .then((value) => { console.log('In third then, the value is ' + value) }) .catch((reason) => { console.log('Request Failed ' + reason) })// /EXAMPLE 1 /** * EXAMPLE2: 在 Promise 的 callback 中回傳另一個 Promise * 留意這裡的 waitASecond 前面有使用 `return` **/ function waitASecond(seconds) { console.log('start', seconds) return new Promise((resolve, reject) => { setTimeout(function() { seconds++ resolve(seconds) }, 1000) }) } waitASecond(0) .then((seconds) => { console.log('In first .then', seconds) return waitASecond(seconds) }) .then((seconds) => { console.log('In second .then', seconds) return waitASecond(seconds) }) .then((seconds) => { console.log('In third .then', seconds) })// /EXAMPLE 2