Reputation: 23407
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.
Upvotes: 0
Views: 4383
Reputation: 129
/*
* 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
}
};
}
Upvotes: 0
Reputation: 22797
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;
}
}
Upvotes: 3
Reputation: 2684
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
});
Upvotes: 0
Reputation: 104399
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)){
....
}
Upvotes: 1