Answer the question
In order to leave comments, you need to log in
How to use a promise value for a condition?
I want to implement lazy loading of a component and use its method mockedClient for a condition. which I pass from the command line.
Lazy loading component:
const mockedClient = () => createMockedClient({
mocks,
typeDefs,
})
export {
mockedClient
}
const getMockedClient = () => import("z-mock-client")
const mocked = getMockedClient ().then(prop => prop.mockedClient()) // initialize ApolloClient
const apolloProvider = new VueApollo({
defaultClient: MOCKED_UI ? mocked : v2,
clients: {
v2: MOCKED_UI ? mocked : v2
}
})
mocked
I get this :Promise {<pending>}
: "resolved"
: ApolloClient
Answer the question
In order to leave comments, you need to log in
Promise value can be obtained either via await or inside then, you can't get it directly as it is executed asynchronously and at the time of assignment
const mocked = getMockedClient ().then(prop => prop.mockedClient())
it has not yet executed , it's just a declaration of intent.
That is, either
then the code will wait for the promise to be executed, before the name of the function inside which await is used, you need to put async
or
getMockedClient ().then(prop => {
const mocked = getMockedClient ()
// И тут делаете с полученным значением что хотите
})
Well, it’s like a promise is returned to you and it will resolve sometime later than your piece of code.
Why not just something like this:
const mocked = require("z-mock-client")()
const apolloProvider = new VueApollo({
defaultClient: MOCKED_UI ? mocked : v2,
clients: {
v2: MOCKED_UI ? mocked : v2
}
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question