Skip to content

Instantly share code, notes, and snippets.

@pkellner
Created May 1, 2022 15:10
Show Gist options
  • Save pkellner/23099cc6b3e0854715f75bf6d5801e80 to your computer and use it in GitHub Desktop.
Save pkellner/23099cc6b3e0854715f75bf6d5801e80 to your computer and use it in GitHub Desktop.
import { fetchCities } from "../data/cities";
export function fetchCityListData(displayCount) {
const cityPromise = fetchCities(displayCount);
return {
cities: wrapPromise(cityPromise),
};
}
function wrapPromise(promise) {
let status = "pending";
let result;
let suspender = promise.then(
(r) => {
status = "success";
result = r;
},
(e) => {
status = "error";
result = e;
}
);
return {
read() {
if (status === "pending") {
throw suspender;
} else if (status === "error") {
throw result;
} else if (status === "success") {
return result;
}
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment