Nextjs:运行 完成获取后的代码
Nextjs: running code after complete fetch
这是我的代码:
async function checkAuth() {
console.log("3")
await fetch(apiUrl+'/auth', {
method: 'POST'
}).then(response => response.json()).then(result => {
console.log("5")
}).catch(error => {
console.log("6")
})
console.log("4")
}
function getFirstData() {
console.log("1");
checkAuth()
console.log("2");
}
我想先checkAuth()
运行,完成后,另一个代码是运行。
所以,我的结果应该是:
13542
但是我的代码不起作用
试试这个。
async function getFirstData() {
console.log("1");
await checkAuth()
console.log("2");
}
这是我的代码:
async function checkAuth() {
console.log("3")
await fetch(apiUrl+'/auth', {
method: 'POST'
}).then(response => response.json()).then(result => {
console.log("5")
}).catch(error => {
console.log("6")
})
console.log("4")
}
function getFirstData() {
console.log("1");
checkAuth()
console.log("2");
}
我想先checkAuth()
运行,完成后,另一个代码是运行。
所以,我的结果应该是: 13542
但是我的代码不起作用
试试这个。
async function getFirstData() {
console.log("1");
await checkAuth()
console.log("2");
}