类似的东西
const getJson = async (url) => {
const res = await fetch(url)
return res.json()
}
const yourFetch = async () => {
try {
const json = await getJson('/i/1.json')
if (json.key) {
const json2 = await getJson('/i/2.json')
return json2.key
}
throw new Error('No key')
} catch (err) {
console.error(err)
}
}
,
func subscribeToResponse() {
homeViewModel.filterModelObservable.bind(to: self.tableView.rx.items(cellIdentifier: HomeTableViewCell.reuseIdentifier,cellType: HomeTableViewCell.self)) { row,books,cell in
cell.separatorInset = UIEdgeInsets(top: 0,left: 0,bottom: 20,right: UIScreen.main.bounds.width)
cell.titleLabel.text = books.name
cell.secondaryTitleLabel.text = books.type
cell.selectionStyle = .none
if books.available == true {
cell.avalibaleOrNotLabel.text = "Avalibale"
cell.avalibaleOrNotStatus.backgroundColor = .green
} else {
cell.avalibaleOrNotLabel.text = "Not Avalibale"
cell.avalibaleOrNotStatus.backgroundColor = .gray
}
}.disposed(by: disposeBage)
}
(模块除外)仅在异步函数内有效。所以让我们创建一个 Async IIFE
如果您预先知道路径,您可以选择Promise.all:
const url = "https://jsonplaceholder.typicode.com";
const getJson = async url => await fetch(url).then(res => res.json());
;(async () => { // Async IIFE
const getJson1 = await getJson(`${url}/users/1`);
if (!getJson1.id) throw new Error('No ID');
const getJson2 = await getJson(`${url}/todos/${getJson1.id}`);
console.log(getJson2);
})();
console.log("PS: I don't wait for the above to finish");
本文链接:https://www.f2er.com/1173.html