promise-6

const tasks = [
  { name: 'task-1', time: 200 },
  { name: 'task-2', time: 400},
  { name: 'task-3', time: 10},
  { name: 'task-4', time: 600}
];

function getPromise(task) {
  const taskPromise = new Promise((resolve, reject) => {
    console.log(task.name + " started.");
    
    setTimeout(() => {
      console.log(task.name + " done in ", task.time, "ms.");
      resolve(task.name);
    }, task.time);
  });
  
  return taskPromise;
}

function executeTasks(tasks) {
  return tasks.reduce((accumulator, task) => {
    return accumulator.then(getPromise(task));
  }, Promise.resolve());
}

executeTasks(tasks);