简体   繁体   中英

Possible unhandled promise rejection

I'm trying to reject a promise as explained in the documentation of the API of the framework I'm using (Apollo stack) but it doesn't show an example, it only states just that I have to reject the promise if there is an error, and I'm trying to get rid of the annoying YellowBox message "Warning: Possible unhanded promise rejection" when trying my application without an internet connection.

My method actually works, it goes to the catch and it shows the error message, but i keep getting the annoying YellowBox message, that's what I'm trying to fix.

first thing I did, goes to the catch, as expected, but it shows a YellowBox message Warning: Possible unhandled promise rejection...

return client.query({ query: gql`...`, }).then((data) => {
    console.log(data);
    data;
}).catch((error) => {
    console.log(error);
    error;
});

Last thing I've tried:

var promise = new Promise(function(resolve, reject) {
  //async call, client.query(..) "returns a promise that should be rejected
  //if there is an error message..."
  client.query({ query: gql`...`, }).then(({data}) => {
    console.log(data);
    resolve(data);
  }).catch((error) => {
    console.log(error); // goes right here, works.
    reject(error.message);
  });
});
//just trying this out
promise.then((data) => {
  console.log(data);
}).catch((error) => {
  console.log(error); 
});

Also, adding the tag Meteor because couldn't find Apollo but it's pretty much the same thing.

Trying more stuff as suggested in the answers and comments:

var promise = new Promise(function(resolve, reject) {
  client.query({
    query: gql`...`,
  }).then(({data}) => {
    console.log(data);
    resolve(data);
  }).catch((error) => {
    reject(error.message);
  });
}, (error) => {
  console.log(error);
});

another:

var callback = {
  success: function(data) {
    console.log("SUCCESS");
  },
  error: function(data) {
    console.log("ERROR");
  }
};

var promise = new Promise(function(resolve, reject) {
  client.query({
    query: gql`...`,
  }).then(({data}) => {
    console.log(data);
    resolve(data);
  }).catch((error) => {
    console.log(error);
    reject(error.message);
  });
  return promise;
});
promise.then(callback.success, callback.error);

another:

client.query({
  query: gql`...`,
}).then(({data}) => {
  console.log(data);
}, (error) => {
  console.log(error);
});

ApolloStack: http://docs.apollostack.com/apollo-client/network.html it says, that returns a promise that should be rejected if an error occurs.

YellowBox detects unhandled promises and such things and throws warnings.

There's no reason to create a promise if client.query does it for you...

// no new Promise here, just make the query

return client.query({ query: gql`...`, }).then((data) => {
    console.log(data);
    data;
}).catch((error) => {
    console.log(error);
    error;
});

Found the issue, the framework is working on it at the moment, a fix will come soon, so there is no right answer for now.

I will copy-paste the right way (as shown in the question). It's also copied from the official frameworks' documentation that shows how to do it, so the next person coming with the same problem will know they will have to wait a couple of days until they fix it.

client.query({ query: gql`...`, }).then((data) => {
    console.log(data);
}).catch((error) => {
    console.log(error);
});

try to handle error in rejection callback instead of catch callback:

var promise = new Promise(function(resolve, reject) {

    client.query({ query: gql`...`, }).then(({data}) => {
        console.log(data);
        resolve(data);
    }, (error)=>{
        console.log(error); // goes right here, works.
        reject(error.message);
    })
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM