Comments (4)
You're quite right that this might be a bit confusing. Ultimately, it's a pretty standard beginners stumbling block with callbacks. Many beginners often try to write something like:
try {
setTimeout(function () {
throw new Error()
}, 1000)
} catch (err) {
}
and are then confused when it does not work.
The issue here is the same, and in order to be fully beginner friendly it would be great to document it. I'd actually really welcome a pull request with a good explanation of this behavior and how to avoid it. Do you want to try? Otherwise I'll try to write it up on my own, but I'm not quite sure how I'd explain the concepts here in the most beginner-friendly terms.
from vue-async-computed.
Sorry if I've misunderstood, but I've been caught by something similar too. Isn't the code from your first example:
setTimeout(() => resolve('Hello ' + this.username + '!'), 1000)
just not getting the correct value of this
because of the arrow function definition?
from vue-async-computed.
@foxbenjaminfox Sorry I don't feel confident doing a pull request eplaining this issue because I don't feel confident enough with the English language.
@dsl101 No, in this case this
is referencing the context of the greeting
function, the Vue instance. This is expected.
from vue-async-computed.
@Yovar: That's fine. I'll try and think of how I can best write it up myself. Thank you for raising this issue, in any case.
@dsl101: As @Yovar says, the problem here isn't the arrow function. this
is supposed to be the Vue instance, and the arrow function lets it remain the Vue instance.
The problem is that inside a callback inside the greeting
function isn't (synchronously) part of the greeting
function, so Vue cannot register the property accesses there as part of greeting
s dependencies.
If all that seems a bit too abstract, look at this simple example:
try {
setTimeout(function () { throw new Error() }, 1000)
} catch (err) {
console.error(err)
}
Will it log the error or not?
You can try running it, and see that it won't. The bit that throws the error in the callback, isn't actually within the try
block. When the callback finally gets called a second later, the try
block is long gone.
Imagine if the code was written this equivalent way:
const cb = function () { throw new Error() }
try {
setTimeout(cb, 1000)
} catch (err) {
console.error(err)
}
This is the same thing, and perhaps now it's a bit clearer why the catch
block won't catch the thrown error. The same principle applies not just with try/catch
, but also with Vue's dependency detection.
from vue-async-computed.
Related Issues (20)
- Typescript / vue-async-computed Property 'foo' does not exist on type 'CombinedVueInstance<Vue, {...} HOT 2
- Doesn't seem to deep watch properties. HOT 2
- Initial State of Async Call 'updating' set to true when using shouldUpdate()
- Vue 3 support? HOT 5
- Add support to installation as mixin.
- Make Promise as return of method update() or new method updatePromise()
- Provide option to exclude or explicitly list watched properties
- Value getting computed on route changes
- kjidjvtlcdgjj
- Use with Nuxt? HOT 1
- Provide cancel method
- implement set like get
- "this.$options is undefined" is being raised when using vue async computed
- "lazy: true" fails to stop updates HOT 1
- TypeScript support in 3.8.0 HOT 4
- Add vue-async-computed-decorator to README.md HOT 2
- Issue updating reactive variables in asyncComputed HOT 1
- asyncComputed return only default value HOT 2
- how to use this plugin with class based vue components? HOT 2
- Executed without being called, normal computed works different HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-async-computed.