admin管理员组文章数量:1401672
I have this rather simple Vue
ponent (stripped of unnecessary parts):
Vueponent('equipment-tree', {
data(){
return {
tree: [],
}
},
template: `
<template>
<div id="equipment_tree"></div>
</template>`,
mounted: function(){
this.getTreeView()
console.log('4. TREE LOADED AND VISIBLE');;
},
methods: {
setUpTree(){
$("#equipment_tree").jstree("destroy");
$('#equipment_tree').jstree({
core : {
data : this.tree,
multiple: false,
themes: {
dots: true
}
},
});
console.log("2. TREE SET UP")
},
getTreeView(){
fetch("myurl /*just a URL */",
{
method: 'GET',
})
.then((response) => response.json())
.then((data) => {
console.log('1. GOT DATA');
this.tree = JSON.parse(data.tree);
this.setUpTree();
console.log('3. SET UP COMPLETE');
})
}
}
})
On mount, I call the method getTreeView()
that gets the data from the database, saves it in the variable tree
, and then calls setUpTree()
which creates a tree using the jstree
library. When I run it in the log I see
4. TREE LOADED AND VISIBLE
1. GOT DATA
2. TREE SET UP
3. SET UP COMPLETE
that is, the flow continues after the call to getTreeView()
. Now, what if I want to wait until getTreeView()
finishes so that the log 4. TREE LOADED AND VISIBLE
gets actually printed last?
I tried with async/await
as follows: I changed
mounted: function(){
this.getTreeView()
console.log('4. TREE LOADED AND VISIBLE');;
}
into
mounted: async function(){
await Promise.resolve(this.getTreeView());
console.log('4. TREE LOADED AND VISIBLE');
}
but I got the same result as before. Same if following the answers to this question.
How can I wait for the method getTreeView()
to finish?
Please, note that this is a reduced example because I want to understand how it works, not just because the order of log matters.
I have this rather simple Vue
ponent (stripped of unnecessary parts):
Vue.ponent('equipment-tree', {
data(){
return {
tree: [],
}
},
template: `
<template>
<div id="equipment_tree"></div>
</template>`,
mounted: function(){
this.getTreeView()
console.log('4. TREE LOADED AND VISIBLE');;
},
methods: {
setUpTree(){
$("#equipment_tree").jstree("destroy");
$('#equipment_tree').jstree({
core : {
data : this.tree,
multiple: false,
themes: {
dots: true
}
},
});
console.log("2. TREE SET UP")
},
getTreeView(){
fetch("myurl /*just a URL */",
{
method: 'GET',
})
.then((response) => response.json())
.then((data) => {
console.log('1. GOT DATA');
this.tree = JSON.parse(data.tree);
this.setUpTree();
console.log('3. SET UP COMPLETE');
})
}
}
})
On mount, I call the method getTreeView()
that gets the data from the database, saves it in the variable tree
, and then calls setUpTree()
which creates a tree using the jstree
library. When I run it in the log I see
4. TREE LOADED AND VISIBLE
1. GOT DATA
2. TREE SET UP
3. SET UP COMPLETE
that is, the flow continues after the call to getTreeView()
. Now, what if I want to wait until getTreeView()
finishes so that the log 4. TREE LOADED AND VISIBLE
gets actually printed last?
I tried with async/await
as follows: I changed
mounted: function(){
this.getTreeView()
console.log('4. TREE LOADED AND VISIBLE');;
}
into
mounted: async function(){
await Promise.resolve(this.getTreeView());
console.log('4. TREE LOADED AND VISIBLE');
}
but I got the same result as before. Same if following the answers to this question.
How can I wait for the method getTreeView()
to finish?
Please, note that this is a reduced example because I want to understand how it works, not just because the order of log matters.
Share Improve this question edited May 23, 2022 at 17:55 Nikola Pavicevic 23.5k9 gold badges29 silver badges51 bronze badges asked Sep 26, 2021 at 21:48 LibraLibra 3695 silver badges16 bronze badges 2- Is there any reason not to make gettreeview asynchronous so you can await it directly? – 2e0byo Commented Sep 26, 2021 at 21:54
- 2 This is not a vue thing this is how async code works. Your function getTreeView is dealing with promises, it executes right away but the side effects it results in happen when the promise is resolved. – Robert Commented Sep 26, 2021 at 22:15
1 Answer
Reset to default 5Try to wait for method too:
async getTreeView(){
await fetch("myurl /*just a URL */",
{
method: 'GET',
})
.then((response) => response.json())
.then((data) => {
console.log('1. GOT DATA');
this.tree = JSON.parse(data.tree);
this.setUpTree();
console.log('3. SET UP COMPLETE');
})
}
}
in mounted hook:
async mounted(){
await this.getTreeView();
console.log('4. TREE LOADED AND VISIBLE');
}
本文标签: javascriptAsyncAwait method in VuejsStack Overflow
版权声明:本文标题:javascript - AsyncAwait method in Vue.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744310648a2600010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论