• 追加された行はこの色です。
  • 削除された行はこの色です。
#author("2019-11-04T06:15:47+09:00","default:ryuichi","ryuichi")
* 02. 処理のチェーン [#xbde4db9]
#author("2019-11-04T14:44:28+09:00","default:ryuichi","ryuichi")
* 02. Promise処理群のチェーン実行 [#xbde4db9]

** Promise処理群の作成 [#b0d1ddd6]

 const task1 = () => {
   return new Promise((resolve, reject) => {
     // ここに処理1の処理を書く
     resolve("Task 1 is done.");
   });
 };
 
 const task2 = (message) => {
   return new Promise((resolve, reject) => {
     // ここに処理2の処理を書く
     resolve(message + " Task 2 is done.");
   });
 };
 
 const task3 = (message) => {
   return new Promise((resolve, reject) => {
     // ここに処理3の処理を書く
     resolve(message + " Task 3 is done.");
   });
 };


** Promise処理群のチェーン実行(連結実行) [#b8a94101]

 task1().then((result) => {
     return task2(result);
 }).then((result) => {
     return task3(result);
 }).then((result) => {
     console.log(result + " Finished.");
 });

** then()の返却値はPromiseオブジェクトでなくともよい [#x3af5773]

 task1().then((result) => {
     return task2(result);
 }).then((result) => {
     return task3(result);
 }).then((result) => {
     return result + " Task A is done.";
 }).then((result) => {
     console.log(result + " Finished.");
 });

- ここでは'''return result + " Task A is done.";'''はPromiseオブジェクトではないが、then()はPromiseオブジェクトに変換して次のthen()につなげる

** 参考 [#af85f0cb]

https://ja.javascript.info/promise-chaining


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS