React-native-contacts: suggestion: move to promises

Created on 27 Mar 2017  路  10Comments  路  Source: morenoh149/react-native-contacts

Would it make sense to move the api to use promises rather than callbacks? I'm not exactly sure when people tend to use one over the other, possibly when there are some background tasks involved in the native code. Which I believe we do have in here.

Most helpful comment

My solution in the case anybody needs promises for _redux-saga_ or some other async code:

function getContacts() {
    return new Promise(resolve => {
        Contacts.getAll((error, contacts) => {
            if (error) {
                throw error;
            } else {
                resolve(contacts);
            }
        });
    });
}

And then just call:

const contacts = yield call(getContacts);

All 10 comments

I think promises are pretty popular. There are some js folks I know that still prefer callbacks. Is it hard to support both?

I'm sure you could support both, but I think it might lead to confusion.

I've started using the async / await syntax in my RN apps which makes using promises a lot nicer, not so much nasty boilerplate code. And neater than callbacks too.

Any progress about promise?

I did some tests modifying the project in my own node_modules directory and it ran nice on android. In my opinion promises are cleaner especially when using async/await. +1 to support promises.

My solution in the case anybody needs promises for _redux-saga_ or some other async code:

function getContacts() {
    return new Promise(resolve => {
        Contacts.getAll((error, contacts) => {
            if (error) {
                throw error;
            } else {
                resolve(contacts);
            }
        });
    });
}

And then just call:

const contacts = yield call(getContacts);

If the collaborators are ok with it, I may take a whack at promisifying the library. I will ensure that callbacks are supported as well, for backwards compatibility.

@jacob-israel-turner absolutely, please submit a PR :)

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

any progress?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

duranmla picture duranmla  路  5Comments

dualcyclone picture dualcyclone  路  6Comments

rafaumlemos picture rafaumlemos  路  4Comments

uriva picture uriva  路  6Comments

krishna201 picture krishna201  路  4Comments