How to use yield call in React-Native?

Viewed 4560

I am implementing fingerprint scanning in my RN app and I found a good tutorial for that but the code there has a syntax which I have never used - yield call(), however, I googled it and couldn't find a proper explanation for it. Here is the code:

if (isFingerPrintSupported === true) {
                        yield call(KeychainService.setCredentials, user_name, 
                                          JSON.stringify({ password }));
                    }

Is there something else I can use instead in this case? if not then how can I import this or install in order to make it work?

EDIT(example code added):

   componentWillMount() {
    let credentials = yield call(KeychainService.getCredentials);
    if (credentials && credentials.username)) {
        let isFingerPrintSupported = yield call(KeychainService.checkBiometricSupportednEnrolled);

        if (isFingerPrintSupported === true) {
            // show fingerprint alert on login page
            // and authenticate FingerPrint when user touch the sensor
        }
    } else {
        // else don’t show fingerprint option on login
    }
}
1 Answers

yield can be used inside a generator function & it helps to pause and resume a function at any time asynchronously. Also Additionally it helps to return value from a generator function.

Check this document for more information.

call is redux-saga effects which help to make asynchronous calls. Check this for more information.

import { call } from 'redux-saga/effects'

function* authorize(user, password) {
  try {
    const response = yield call(/** Api call */, user, password)
    ...
  } catch(error) {
    ...
  }
}

Note

If you don't want to use yield, you can directly call you API with params using axios or fetch.

Hope this helps you. Feel free for doubts.

Related