It is a mistake to Promise?

Welcome!

For exercise write the code that simulates the execution of the animation step by step, used Promise. In this connection 2 questions:

1. Do I understand the logic of the Promise? If not, tell me how to write this code...
function animation(step, delay, log) {
 return new Promise((resolve, reject) => {
 if(typeof delay === "number") {
 setTimeout(() => {
 resolve(log(`Animation No.:${step} executed successfully`));
 }, delay);
 } else reject(log(`Animation No.:${step} not completed`));
});
}

function log(result) {
 console.log(`Log: ${result}`);
}

animation(1, 500, log)
 .then(animation(2, 1000, log))
 .then(animation(3, 2000, log));

// Log: Animation number:1, completed successfully
// Log: Animation number:2, completed successfully
// Log: Animation number:3, completed successfully


2. Trying to simulate the error in step 4, passing not a number, and string. Why, then step 4 performs a forward 3, and what is the error?
function animation(step, delay, log) {
 return new Promise((resolve, reject) => {
 if(typeof delay === "number") {
 setTimeout(() => {
 resolve(log(`Animation No.:${step} executed successfully`));
 }, delay);
 } else reject(log(`Animation No.:${step} not completed`));
});
}

function log(result) {
 console.log(`Log: ${result}`);
}

animation(1, 500, log)
 .then(animation(2, 1000, log))
 .then(animation(3, 2000, log))
 .then(animation(4, "500", log));

// Log: Animation number:4, not met
// Uncaught (in promise) undefined
// Log: Animation number:1, completed successfully
// Log: Animation number:2, completed successfully
// Log: Animation number:3, completed successfully
March 23rd 20 at 19:02
2 answers
March 23rd 20 at 19:04
Solution
You don't understand the promises and JS. This is the main mistake.

I'll try a little bit to explain.
fn(a,b,c).then(fn(k,m,n))
That's how you write. What is happening here.
fn is triggered and immediately returns an object of type promise, which you call the then method. This method takes two functions for success and fail. As the success function, you specify the expression fn(k,m,n). Okay, then JS evaluates this expression, that is, literally performs this function and as a result receives a new promise, which is used as a callback function for the success of the first promise.

Carefully reread the documentation.
https://learn.javascript.ru/promise

The bug fix will be something like this:
animation(1, 500, log)
 .then(e=>animation(2, 1000, log))
 .then(e=>animation(3, 2000, log))
 .then(e=>animation(4, "500", log));
March 23rd 20 at 19:06
Solution
then waiting at the entrance callback, i.e. function.
And you shoved him promis, and even start all these promises at the same time.

If you do so
.then(() => animation(2, 1000, log))
the animation will be executed when resolvida promis -> will callback, which runs next animation

Find more questions by tags JavaScript