Sobes.tech
Middle

Practical task on Promise (the task was announced but not fully disclosed in the transcript).

sobes.tech AI

Answer from AI

Promise in JavaScript is used for handling asynchronous operations. A practical task might involve, for example, executing multiple asynchronous actions sequentially or handling errors.

Example: sequential execution of two asynchronous functions using Promise:

function asyncTask1() {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('Task 1 completed');
      resolve(1);
    }, 1000);
  });
}

function asyncTask2(prevResult) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('Task 2 completed, received:', prevResult);
      resolve(prevResult + 1);
    }, 1000);
  });
}

asyncTask1()
  .then(result => asyncTask2(result))
  .then(finalResult => console.log('All tasks completed, result:', finalResult))
  .catch(error => console.error('Error:', error));

This approach allows managing asynchronous code sequentially and handling possible errors.

Practical task on Promise (the task was announced but… - sobes.tech