I've been looking through the code but can't figure out a good way to use await with Contacts.getAll. I want to be able to do something similar to what React Native uses for AsyncStorage (https://facebook.github.io/react-native/docs/asyncstorage.html#content)
E.g. code like this
var contacts = await Contacts.getAll((err, contacts) => {
if(err && err.type === 'permissionDenied') {
console.log("Permission denied contacts");
return [];
} else {
return contacts;
}
});
for (var i = 0; i < contacts.length; i++) {
var contact = contacts[i];
// do stuff here
}
Do you have any ideas on how to do this or would it require some changes to the project code? I think it isn't supported currently.
are you putting that code within a function declared as async?
also async/await only works for functions that return Promises. See https://www.twilio.com/blog/2015/10/asyncawait-the-hero-javascript-deserved.html
@pickhardt I believe promise and by extension async/await can be added fairly easily: https://medium.com/the-exponent-log/react-native-meets-async-functions-3e6f81111173#.odjz3awk0 although I have not looked into what it might take to preserve compatibility with callbacks.
PR's accepted
@rt2zz that blog post suggests that RN modules should use async moving forward. But the callback style works just fine for me.
Would love promise support, as that's how I'm using async actions in my app :)
Will attempt a PR soon
Sorry for double post, I'm on my phone. Would you accept a PR that doesn't allow callback compatibility?
Pretty sure async/await aka promise support would just require another api endpoint (as far as the android api is concerned). In java we support overloading methods and we could simply declare another getAll() that is invoked without a callback. This version would instead accept and resolve a promise. See native modules android #promises
ping @rt2zz
@morenoh149 I think we could alternatively promisify the callbacks in the js code and _always_ return a promise in addition to triggering the callback if present.
Promisify of Contacts.getAll using js
function getContacts() {
return new Promise((resolve, reject) => {
Contacts.getAll((err, contacts) => {
if (err) {
reject(err);
} else {
resolve(contacts);
}
});
});
}
I'll add this soon https://facebook.github.io/react-native/docs/native-modules-ios#promises seems straightforward.
I'd like to takeup this issue.
@utkarsh-dixit go for it. Should be easy
To use this great component with Redux, I needed to make it return promises (rather than callbacks). Using the hints in this thread and other resources, I came up with a solution for calling this component from a Redux's Action (Action Creator).
Please, refer to the following question on StackOverflow.
https://stackoverflow.com/questions/58126240/in-a-reduxs-action-calling-a-function-with-callbacks
Hey @utkarsh-dixit, are you still working on this? If not, I'll take a crack at promisifying all of the methods. I can either add code to each one to accept a callback or be used with async/await (which I think would be easier) or I can add new methods like getContactsAsync. I'm leaning towards the former, but do you have a preference @morenoh149 (or anyone on this thread)?
@zingerj please feel free to take this over. In oss things tend to be dropped.
I have no preference on implementation (I think). So lets go with your preference.
This issue is stale, please provide more information about the status
any progress?
no progress. Please submit a PR.
Most helpful comment
Promisify of
Contacts.getAllusing js