Reputation: 34517
Hi I am trying to call async function makeRemoteExecutableSchema
which returns promise.
async function run() {
const schema = await makeRemoteExecutableSchema(
createApolloFetch({
uri: "https://5rrx10z19.lp.gql.zone/graphql"
})
);
}
I am calling this function in the constructor.
class HelloWorld {
constructor() {
try {
run();
} catch (e) {
console.log(e, e.message, e.stack);
}
}
}
I am getting this error. Does anyone know how to resolve this ?
(node:19168) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'getQueryType' of undefined
(node:19168) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Upvotes: 2
Views: 4475
Reputation: 708106
If makeRemoteExecutableScheme()
returns a promise that eventually rejects, then you have no code to handle that rejection. You can handle it one of two ways:
async function run() {
try {
const schema = await makeRemoteExecutableSchema(
createApolloFetch({
uri: "https://5rrx10z19.lp.gql.zone/graphql"
})
);
} catch(e) {
// handle the rejection here
}
}
Or here:
class HelloWorld {
constructor() {
run().catch(err => {
// handle rejection here
});
}
}
You use try/catch
around an await
and within the same function. One run()
has returned, you're just dealing with a promise at that point so you would catch the rejection there with .catch()
, not with try/catch
.
It's important to remember that await
is syntactical sugar for .then()
within a function only. It does not apply any magic beyond that function. Once run()
returns, it's just returning a regular promise so if you want to catch the rejection from that returned promise, you have to either use .catch()
on it or await
it again and then surround it with try/catch
. Surrounding a non-awaited promise with try/catch
does not catch rejected promises which is what you were doing.
Upvotes: 4