0

Friend I will take one class for define to all function. Now We required some of the function with its callback. So I define as below.

CallBack define :

export const getStoredData(key: string, callback?: ?(error: ?Error, result: ?string)) =>{
      try {
      const value = await AsyncStorage.getItem(key);
      if (value !== null){
        return value
      }
    } catch (error) {
      return error
    }
}

Call the function as below :

   getStoredData('apple' , (error , result) =>{
     if (error) {
       console.log('error is = ', error);
     } else {
       console.log('result is = ', result);
     }
   });

But i have error to define function unexpected token.

Please help me.

Val
  • 21,938
  • 10
  • 68
  • 86
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112

4 Answers4

3

As @MayankShukla said, you aren't using arrow function correctly.

To fix your expression, you also need to add =>void as below to fix the function type of callback,

export function getStoredData(key: string, callback?: ?(error: ?Error, result: ?string) => void ) {

In my vs code Unexpected Identifier disappeared with above code.

Edited for comment 1: for getting result from callback, try change the function

export async function getStoredData(key: string, callback?: ?(error: ?Error, result: ?string) => void ) {
    try {
        const value = await AsyncStorage.getItem(key);
        if (value !== null) {
            callback(null, value);
            return value;
        }
    } catch (error) {
        callback(error, null);
        return error;
    }
}
Val
  • 21,938
  • 10
  • 68
  • 86
  • yes I update, but call the function, nothing get error or result. – Kirit Modi Aug 28 '17 at 10:34
  • I have one problem, help me : https://stackoverflow.com/questions/48987496/how-to-create-callback-method-to-get-value-from-another-js#48987658 – Kirit Modi Feb 26 '18 at 11:48
  • App killed API calling issue , Please help me https://stackoverflow.com/questions/51023044/how-to-call-api-when-app-is-killed-in-react-native – Kirit Modi Jun 25 '18 at 12:04
  • @KiritModi I'm currently doing c# wpf, not on react-native project for 8 months. I don't have environment for react-native right now... – Val Jun 27 '18 at 01:49
1

You are using arrow function in a wrong way, you forgot =, Unexpected token is because of this => here:

export const getStoredData(key: string, callback?: ?(error: ?Error, result: ?string)) => {
   ....
}

Either write it like this (note = after getStoredData):

export const getStoredData = (key: string, callback?: ?(error: ?Error, result: ?string)) => { 
    ....
}

or remove the =>

export function getStoredData (key: string, callback?: ?(error: ?Error, result: ?string)){
   ....
}
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
  • check the updated answer, use `export const` with `arrow funciton` or use `function` keyword without `arrow function`, and use any one way. – Mayank Shukla Aug 28 '17 at 09:27
  • but I Will get error unexpected token , expexted =>. – Kirit Modi Aug 28 '17 at 09:29
  • i think you are not using [class transform plugin](https://babeljs.io/docs/plugins/transform-class-properties/) that's why it's throwing error with arrow function, check this answer [how to use plugin for arrow function](https://stackoverflow.com/questions/31362292/how-to-use-arrow-functions-public-class-fields-as-class-methods) – Mayank Shukla Aug 28 '17 at 09:34
  • yes, I will try also second one, in which got error unexpected token , expexted => – Kirit Modi Aug 28 '17 at 09:34
  • but in second method arrow is not there check the answer again, you need to remove the arrow and use function keyword :) – Mayank Shukla Aug 28 '17 at 09:35
0

Your syntax is wrong because you're not exporting a function as you may want. Try assigning your constant and close the brackets properly :

export const getStoredData = (key, callback) => {
  try {
  const value = await AsyncStorage.getItem(key);
  if (value !== null){
    return value
  }
  } catch (error) {
    return error
  }
};

getStoredData('apple', (error, result) => {
 //do stuff
});
Daniel Andrei
  • 2,654
  • 15
  • 16
0
/*

* created by Suresh Mewara

* Date 09-07-2018

*/

import React, {Component} from 'react';
import {AsyncStorage, } from 'react-native';

export class DBPreference {
  // Database key
  static LOGIN_STATUS = 'loginStatus';
  static EULA_STATUS = 'eulaStatus';
  static ACCESS_CODE = 'accessCode';





  static retrieveData = async (key) => {
    try {
      const value = await AsyncStorage.getItem(key);
      if (value !== null) {
        // We have data!!
        console.warn(value);
        return value;
      }
     } catch (error) {
       // Error retrieving data
     }
  }



  static getStoredData = async (key, callback) => {
  try {
  const value = await AsyncStorage.getItem(key);
  if (value !== null){
    callback(null, value);
    console.warn(value);
    return value
  }
  } catch (error) {
    callback(error, null);
    return error
  }
};
}