0

the following is part of my codebase:

const clonePromises = cloneServices?.getAds().map((ad: IAds) => {
   switch(ad.type) {
      case 'tree':
        return api.cloneA(ad.id);
      case 'creativegroup':
        return api.cloneB(ad.id);
      case 'creative':
        return api.cloneC(ad.name, ad.id, ad.hasVariations);
      case 'trackerresource':
        return cloneTracker(ad);
      case 'folder':
        return api.cloneFolder(ad.id).then(null, (err) => {
          let error = 'Unknown error';
          try {
            error =
              JSON.parse(err.responseText)?.message || JSON.parse(err.responseText)?.errors?.message;
          } catch {
            //noop
          }
          return new $.Deferred()
            .resolve({
              folderError: error,
            })
            .promise();
        });
      default:
        // Unable to clone
      }
   }
);
Promise.all(clonePromises)
  .then(async (responses) => {
      return responses;
    })
    .then((responses) => {
        console.log('show this')
   }

This is part of the a function that is triggered by a button handleClick. Promise.all is expected to be triggered after the $.Deferred() operation is triggered and I should see a message called show this in console. Currently, it's working fine, but I want to replace the following jQuery Deferred to promise.

          return new $.Deferred()
            .resolve({
              folderError: error,
            })
            .promise();

I tried the following, but Promise.all operation is not displaying show this in console.

          return new Promise((resolve)=>{
            resolve({
              folderError: error,
            })
          })
hongkongbboy
  • 300
  • 1
  • 5
  • 14
  • That seems correct to me. You can try using the static [`Promise.resolve()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve) method instead, which may make it a bit easier to read. `return Promise.resolve({folderError: error})`. – romellem Nov 15 '22 at 21:11
  • Actually, since you are returning a value from _within_ the promise's `onFullfilled` handler, you can just return that value directly. [If a resolved promise is returned from `onFullfilled`, that promise's value is used](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#return_value). – romellem Nov 15 '22 at 21:14
  • Tried this `return Promise.resolve({folderError: error})` and the `Promise.all` operation is still not triggering afterward. Can you give me guide on how to implement `onFullfilled` in my case? – hongkongbboy Nov 15 '22 at 21:23
  • I added a little more details. – hongkongbboy Nov 15 '22 at 21:34
  • What is `api.cloneFolder`? Does it return a proper promise, or a jQuery deferred/promise? (I'm curious why you use `.then(null, …)` instead of `.catch(…)`) – Bergi Nov 15 '22 at 21:48
  • Have a look at [How to dodge jQuery promises completely when chaining two async jQuery functions?](https://stackoverflow.com/a/31327725/1048572) – Bergi Nov 15 '22 at 22:04
  • @Bergi Thanks for the input, but my supervisor wants me to not use jQuery at all. – hongkongbboy Nov 15 '22 at 22:15
  • @hongkongbboy That's why it is important that you show us the `cloneFolder` code. What you tried, and what romellem suggested, *should* have worked - if there was no jQuery in play any more. – Bergi Nov 15 '22 at 22:41

0 Answers0