0

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.

Computer's Guy
  • 5,122
  • 8
  • 54
  • 74
  • [.then()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) takes a second argument, a function that deals with rejection – Patrick Evans Jul 20 '16 at 18:13
  • yea I already tried passing a function there too, let me try again tho – Computer's Guy Jul 20 '16 at 18:13
  • You added it to the wrong place, you added it as a second argument to `Promise` constructor not `.then()`, should be like: `.then(()=>{},()=>{})` But it might not matter, where is this _"YellowBox message"_? in the Dev Tools console? – Patrick Evans Jul 20 '16 at 18:20
  • Updated the question – Computer's Guy Jul 20 '16 at 18:21
  • Also you do not need to wrap `client.query()` in a promise it already returns a promise – Patrick Evans Jul 20 '16 at 18:23
  • yea that was added later, trying some things – Computer's Guy Jul 20 '16 at 18:25
  • 1
    @Gazta You should probably provide some links to the documentation you're referring to and what exact libraries you're using. To me "YellowBox message" sounds a little too cryptic to figure out what you have in mind. – Tomasz Lenarcik Jul 20 '16 at 18:29
  • 1
    One thing seems certain, the statement, "the promise should be rejected in the case of a network error" CANNOT be inviting you to use the [explicit promise construction antipattern](http://stackoverflow.com/questions/23803743/). That just wouldn't make sense. – Roamer-1888 Jul 21 '16 at 04:05

3 Answers3

1

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;
});
danh
  • 62,181
  • 10
  • 95
  • 136
0

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);
});
Computer's Guy
  • 5,122
  • 8
  • 54
  • 74
-1

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);
    })
 });
MarkoCen
  • 2,189
  • 13
  • 24
  • you put the rejection callback in wrong place, it should be passed as second argument to `client.query({...}).then()`, not to `new Promise()` – MarkoCen Jul 20 '16 at 18:21