Skip to content

Instantly share code, notes, and snippets.

@slorber
Last active July 16, 2018 13:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slorber/2a073ecf4a62100ba4c57452d5736d5d to your computer and use it in GitHub Desktop.
Save slorber/2a073ecf4a62100ba4c57452d5736d5d to your computer and use it in GitHub Desktop.
import { Alert } from 'react-native';
// An alert implementation that returns a promise when the choise is done
export const AlertAsync = (
title,
message,
buttons = [],
options = {},
type,
) => {
return new Promise(resolve => {
const interceptCallback = callback => {
if (!callback) {
resolve();
} else {
const maybePromise = callback();
if (maybePromise instanceof Promise) {
maybePromise.then(resolve, resolve);
} else {
resolve();
}
}
};
Alert.alert(
title,
message,
buttons.map(button => ({
...button,
onPress: () => interceptCallback(button.onPress),
})),
{
...options,
onDismiss: () => interceptCallback(options.onDismiss),
},
type,
);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment