Last active
July 16, 2018 13:21
-
-
Save slorber/2a073ecf4a62100ba4c57452d5736d5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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