promise thenチェーン

const box = document.getElementById("box");
function moveBox(className, delay) {
	const p = new Promise((resolve, reject) => {
		setTimeout(() => {
			box.classList.add(className);
			resolve();
		}, delay);
	});
	return p;     
//  【注意】promiseオブジェクトをリターンしないと、
//                 後続が実行される
}
moveBox("one", 500)
	.then(() => moveBox("two", 500))
	.then(() => moveBox("three", 500))
	.then(() => moveBox("four", 500));

コールバック関数
・引数として渡される関数
・高階関数に渡す関数

高階関数
・関数を受け取る関数

function promiseFactory(count) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      count = count + 2;
      console.log(`カウント:${count} 。時刻:${new Date().toTimeString()}`);
      if (count > 10) {
        reject(count);
      } else {
        resolve(count);
      }
    }, 1000);
  });
}

promiseFactory(0)
  .then(count => { return promiseFactory(count); })
  .then(count => { return promiseFactory(count); })
  .then(count => { return promiseFactory(count); })
  .then(count => { return promiseFactory(count); })
  .then(count => { return promiseFactory(count); })
  .then(count => { return promiseFactory(count); })
  .catch(erroCount => console.log(`エラーカウント:${erroCount}`))
  .finally(() => console.log("終了"))