Skip to content

Instantly share code, notes, and snippets.

@mori-dev
Last active April 11, 2017 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mori-dev/34e882ef0a97759fdd431fc773314897 to your computer and use it in GitHub Desktop.
Save mori-dev/34e882ef0a97759fdd431fc773314897 to your computer and use it in GitHub Desktop.
プロミスでラップ
function* sendPlace(action: Action): Generator<any, any, any> {
try {
const response: any = yield call(Geo.sendPlace, action.payload, action.meta);
yield put(someSendPlaceOk(response));
} catch (error) {
yield put(someSendPlaceNg(error));
}
}
export function* watchGeoSendPlace(): Generator<any, any, any> {
while (true) {
const action = yield take(ActionTypes.SOME_SEND_PLACE_WSSTART);
yield fork(sendPlace, action);
}
}
const Geo = {
sendPlace: (payload: geoSendPlaceStartPayload, meta: any = {}): Promise<*> => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
const newPayload = { lat, lng };
resolve(newPayload, meta)
},
(error) => {
const errorInfo = JSON.stringify(error);
reject(new Error(errorInfo));
},
)
});
},
};
sendCurrentPlace(): void {
const lat = null;
const lng = null;
const payload = {lat, lng};
this.props.geoActions.geoSendPlace(payload);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment