- Published on
模拟异步操作
- Authors
- Name
- duluwa.com
const sleep = async (time: number) => {
await new Promise((resolve) => {
setTimeout(resolve, time)
})
}
const fetchSearchAllPosts = async (keyword: string): Promise<IPost[]> => {
await sleep(2000)
const res = await fetch(`${baseUrl}/posts?title_like=${keyword.trim()}&_sort=id&_order=desc`)
if (!res.ok) {
throw new Error('Failed to search data')
}
return res.json()
}