0

It is a Anti-Pattern to use javascript Promise in this way?

foo (data){
        return new Promise((resolve, reject) => {
          Service.updateFile(data)
              .then((res) => {
                //do someting witch res

                 resolve();
               }).catch((err) => {
                 reject(err);
               });
            });
       }
George
  • 59
  • 2
  • 8
  • 1
    Short answer: Yes. :-) Longer answer: Instead: `function foo(data) { return Service.updateFile(data).then(res => { /* do something with res */ });` (no `catch` at all, no return in `then` handler if you really want to convert the resolution value to `undefined` as the above does). – T.J. Crowder Dec 05 '17 at 14:28
  • 3
    The question is -- why would someone be doing like this when `Service.updateFile` can return a promise? – 31piy Dec 05 '17 at 14:28
  • @31piy Because they did not understand what `then` does, and wanted to create a new promise that is fulfilled with the result of "*doing something with res*" – Bergi Dec 05 '17 at 14:31
  • @31piy Because i want foo function return Promise.resolve() after Service.updateFile completed ? – George Dec 05 '17 at 16:11

0 Answers0