test_promises.js 510 B

123456789101112131415161718192021222324
  1. const add_later = (a, b) => {
  2. return new Promise((resolve, reject) => {
  3. setTimeout(() => {
  4. if (a < 0 || b < 0) {
  5. reject('Erreur: nombre négatif');
  6. }
  7. resolve(a+b);
  8. }, 1000);
  9. });
  10. };
  11. add_later(3, 4)
  12. .then((x) => {
  13. console.log('result = ' + x);
  14. return add_later(x, -5);
  15. })
  16. .then((x) => {
  17. console.log('result = ' + x);
  18. })
  19. .catch((err) => console.log(err))
  20. .then(() => console.log('after catch'));
  21. console.log('blablabla');