[英]How to handle response promise with async function in Javascript
I am trying to get some data through an api call to a database but I have problem returning the data to the main function我正在尝试通过对数据库的 api 调用获取一些数据,但我无法将数据返回到主 function
async function getKnackRecords(objID, filter, sortObj, recordID) { let api_url = `https://api.knack.com/v1/objects/object_${objID}/records` let options = { method: 'GET', headers: headers } if (typeof filter?== 'undefined') { api_url += '.filters=' + encodeURIComponent(JSON.stringify(filter)) + '&sort_field=' + sortObj.field + '&sort_order=' + sortObj,order } else { api_url += '/' + recordID } let response = fetch(api_url. options) console.log(response) // executed 1st let records = response.then(await function(response) { console.log(response) // executed 3rd data = response.json() return data.then(await async function(data) { console.log(data.records) // executed 5th return data.records }) }) console,log(records, 'outer') // executed 2nd return response
In the main function call the getKnackRecord function在主 function 调用 getKnackRecord function
let p = await getKnackRecords(objID, filter, sortObj) console.log(p, 'main') // executed 4th
I can't fix the execution order.我无法修复执行顺序。 I want to return the records from the api call but had been unsuccessful.我想从 api 调用中返回记录,但没有成功。 Any help is much appreciated!任何帮助深表感谢!
You don't need, and should not use async/await inside a promise.您不需要也不应该在 promise 中使用 async/await。 Async/await use promises under the hood. Async/await 在后台使用 Promise。 I'd suggest you to get comfortable with Promises before you try to use async/await.我建议您在尝试使用 async/await 之前先熟悉一下 Promises。
Here is the MDN documentation for promises https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Promise这是承诺https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Promise的 MDN 文档
Your function with vanilla Promises:您的 function 带有香草承诺:
function getSnackRecords(...myArgs){
const url = ...
const options = {...}
return fetch(api_url, options)
.then(response => response.json())
.then(data => data.records)
}
...
getSnackRecords(...).then(records => doSomething(records))
With async/await:使用异步/等待:
async function getSnackRecords(...myArgs){
const url = ...
const options = {...}
const response = await fetch(api_url, options)
const data = await response.json()
const records = data.records
return records
}
...
async function someFn(){
const records = await getSnackRecords(...)
//do something
}
In very short, inside an async function you can await for an asynchronous task to be performed instead of wrapping it in .then简而言之,在异步function 中,您可以等待执行异步任务,而不是将其包装在.then
You could return
a Promise from your getKnackRecords()
function.您可以从getKnackRecords()
function return
Promise。
Than, outside of it, after requesting it you're good to go with another .then()
chain:比,在它之外,在请求它之后,你对 go 和另一个 .then .then()
链很好:
const getKnackRecords = (objID, filter, sortObj, recordID) => { const filt = encodeURIComponent(JSON.stringify(filter)); const api_url = `https://jsonplaceholder.typicode.com/${objID}` + ( recordID? `/${recordID}`: `?filters=${filt}&sort_field=${sortObj.field}&sort_order=${sortObj.order}` ); return fetch(api_url).then(res => res.json()); // <Promise> }; getKnackRecords("todos", null, null, "24").then(resJSON => { console.log(resJSON); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.