Sunny
Sunny

Reputation: 932

Promise resolve even in case of error and trigger then next function

In some cases, 'getData' will throw an error, but I need to resolve it anyhow. How do I resolve 'getData' and ensure 'updateInfo' is called even if 'getData' throws an error?

getData(this.props.data, {})
  .then(
    () => updateInfo('UPDATE_INFO', path, data)
  );

How do I fix this?

Promise.resolve(
  getData(this.props.data, {}))
    .then(
      () => updateInfo('UPDATE_INFO', path, data)
    );

Upvotes: 0

Views: 373

Answers (4)

arizafar
arizafar

Reputation: 3122

If you don't want to use finally either because of an older version of Node or not a bluebird promise.

.then accepts the second parameter for rejection of promise

getData(this.props.data, {})
    .then(
        () => updateInfo('UPDATE_INFO', path, data), (error) => updateInfo('UPDATE_INFO', path, data)
    );

Or implement the same code in .catch as well

getData(this.props.data, {})
    .then(
        () => updateInfo('UPDATE_INFO', path, data)
    ).catch(error => {
        console.log(error)
        return updateInfo('UPDATE_INFO', path, data)
    })

Upvotes: 1

code_monk
code_monk

Reputation: 10130

The proper way to do it would be with Promise.finally(), but if you don't have that, you could trigger your next function in both the then() and catch() phase:

getData(this.props.data, {})
    .then(
        () => updateInfo('UPDATE_INFO', path, data)
    ).catch(
        () => updateInfo('UPDATE_INFO', path, data)
    );

Which you could refactor into:

const update = updateInfo.bind(null,'UPDATE_INFO', path, data);
getData(this.props.data, {})
    .then(update)
    .catch(update);

Or more simply:

const update = updateInfo.bind(null,'UPDATE_INFO', path, data);
getData(this.props.data, {}).then(update,update);

Upvotes: 1

Kai
Kai

Reputation: 2809

One thing that's not immediately clear from your example is if path and data are returned by getData() or not, which changes the answer a bit.

If you want to execute the same callback regardless of whether the returned promise resolves or rejects (and you don't need to get path and data from getData()), you can use Promise.prototype.finally():

getData(this.props.data, {})
  .finally(() => updateInfo('UPDATE_INFO', path, data));

Alternatively, if you want to have different behavior depending on whether the promise resolves or rejects (i.e. path and data are returned by getData()), you can do something like:

getData(this.props.data, {})
  .catch(e => /* handle error */) // If this callback doesn't throw an error or return a promise that rejects, it returns a resolved promise
  .then((path = null, data = {}) => updateInfo('UPDATE_INFO', path, data));

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 281686

Instead of resolving the promise intentionally, if you wish to take an action on both error and success case, you can use .finally on Promise

getData(this.props.data, {}).finally(() => updateInfo('UPDATE_INFO', path, data));

Upvotes: 1

Related Questions