-1

const randomIntegerFromInterval = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
};

const makeTransaction = (transaction) =>
  new Promise((res, rej) => {
    const delay = randomIntegerFromInterval(200, 500);
    setTimeout(() => {
      const canProcess = Math.random() > 0.3;
      if (canProcess) {
        res(transaction.id, randomIntegerFromInterval(200, 500));
      } else {
        rej(transaction.id);
      }
    }, delay);
  });

const logSuccess = (id, time) => {
  console.log(`Transaction ${id} processed in ${time}ms`);
};

const logError = (id) => {
  console.warn(`Error processing transaction ${id}. Please try again later.`);
};

makeTransaction({ id: 70, amount: 150 }).then(logSuccess).catch(logError);

makeTransaction({ id: 71, amount: 230 }).then(logSuccess).catch(logError);

makeTransaction({ id: 72, amount: 75 }).then(logSuccess).catch(logError);

makeTransaction({ id: 73, amount: 100 }).then(logSuccess).catch(logError);

For some reason, I keep getting an undefined value. Haven't find out why it's going on like that.

the output is has to be something like. Please explain what is the problem, or what should I change.

task_3.js:23 Error processing transaction 73. Please try again later.
task_3.js:19 Transaction 70 processed in 458ms
task_3.js:19 Transaction 72 processed in 354ms
task_3.js:23 Error processing transaction 71. Please try again later.
Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
ElMuchacho
  • 300
  • 1
  • 12
  • 4
    `res` only takes one argument, so `randomIntegerFromInterval(200, 500)` is ignored, for one. – Ry- Jun 16 '20 at 05:58
  • Please checkout: [How do you properly return multiple values from a promise?](https://stackoverflow.com/a/28703689) and [Can promises have multiple arguments to onFulfilled?](https://stackoverflow.com/q/22773920) – palaѕн Jun 16 '20 at 06:08

2 Answers2

1

Resolve the promise like this

res({
  id: transaction.id, 
  time: randomIntegerFromInterval(200, 500)
});

and log like that

const logSuccess = ({id, time}) => {
  console.log(`Transaction ${id} processed in ${time}ms`);
};
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

It's because executor function callbacks res and rej accept only a single parameter. You need to change it to something like this:

const randomIntegerFromInterval = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
};

const makeTransaction = (transaction) =>
  new Promise((res, rej) => {
    const delay = randomIntegerFromInterval(200, 500);
    setTimeout(() => {
      const canProcess = Math.random() > 0.3;
      if (canProcess) {
        res({id: transaction.id, time: randomIntegerFromInterval(200, 500)});
      } else {
        rej(transaction.id);
      }
    }, delay);
  });

const logSuccess = ({id, time}) => {
  console.log(`Transaction ${id} processed in ${time}ms`);
};

const logError = (id) => {
  console.warn(`Error processing transaction ${id}. Please try again later.`);
};

makeTransaction({ id: 70, amount: 150 }).then(logSuccess).catch(logError);

makeTransaction({ id: 71, amount: 230 }).then(logSuccess).catch(logError);

makeTransaction({ id: 72, amount: 75 }).then(logSuccess).catch(logError);

makeTransaction({ id: 73, amount: 100 }).then(logSuccess).catch(logError);

For a reference, see this:

The signatures of these two functions are simple, they accept a single parameter of any type.

Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
  • Sebastian, can I ask you : what is the difference between 'return new Promise', and just 'new Promise' like I did? – ElMuchacho Jun 16 '20 at 06:15
  • The difference is that when you `return` a Promise, you can chain it (call `func().then().catch()`). Without returning a Promise you wouldn't be able to do that. And the truth is that you also **did** return a Promise because the arrow function does that for you in your case. That's why you are able to do `.then().catch()` on your function call – Sebastian Kaczmarek Jun 16 '20 at 06:30