Skip to content

Files

Latest commit

 

History

History
465 lines (372 loc) · 7.52 KB

API.md

File metadata and controls

465 lines (372 loc) · 7.52 KB

Local Async Storage API

Table of Contents

setItem

Sets a value for a key and invokes a (optional) callback once completed.

Method:

static setItem(key, value, [callback])

Return:

A Promise object.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
key string Yes Key of the item to set.
value string Yes Value to set for the key.
callback ?(error: ?Error) => void No Function that will be called with any error.

Example:

setItem = async () => {
  try {
    await AsyncLocalStorage.setItem('@key', 'value')
  } catch(e) {
    // error
  }
}

getItem

Fetches an item for a given key and invokes (optional) callback once completed.

Method:

static getItem(key, [callback])

Return:

A Promise with item, if exists, null otherwise.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
key string Yes Key of the item to fetch.
callback ?(error: ?Error, result: ?string) => void No Function that will be called with a result if found or any error.

Example:

getItem = async () => {
  let value

  try {
    value = await AsyncLocalStorage.getItem('@key')
  } catch(e) {
    // error
  }

  console.log(value)

  /*
    output: 
    value
  */
}

removeItem

Removes an item for a key, and invokes (optional) callback once completed.

Method:

static removeItem(key, [callback])

Return:

A Promise object.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
key string Yes Key of the item to remove.
callback ?(error: ?Error) => void No Function that will be called with any error.

Example:

removeItem = async () => {
  try {
    await AsyncLocalStorage.removeItem('@key')
  } catch(e) {
    // error
  }
}

clearStorage

Erases all AsyncLocalStorage for all clients, libraries, etc. You probably don't want to call this; use removeItem or removeMultiple to clear only your app's keys.

Method:

static clearStorage([callback])

Return:

A Promise object.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
callback ?(error: ?Error) => void No Function that will be called with any error.

Example:

clearStorage = async () => {
  try {
    await AsyncLocalStorage.clearStorage()
  } catch(e) {
    // error
  }
}

getKeys

Returns all keys known to your App, for all callers, libraries, etc. Once completed, invokes (optional) callback with errors (if any) and array of keys.

Method:

static getKeys([callback])

Return:

A Promise object.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
callback ?(error: ?Error, keys: ?Array) => void No Function that will be called with all keys found and any error.

Example:

getKeys = async () => {
  let keys = []

  try {
    keys = await AsyncLocalStorage.getKeys()
  } catch(e) {
    // error
  }

  console.log(keys)

  /*
    output: 
    ["@key"]
  */
}

setMultiple

Stores multiple key-value pairs in a batch. Once completed, callback with any errors will be called.

Method:

static setMultiple(keyValuePairs, [callback])

Return:

A Promise object.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
keyValuePairs Array Yes Array of key-value object for the items to set.
callback ?(errors: ?Array) => void No Function that will be called with an array of any key-specific errors found.

Example:

setMultiple = async () => {
  const firstPair = { key1: 'hello1' };
  const secondPair = { key2: 'hello2' };

  try {
    await AsyncLocalStorage.setMultiple([value1, value2])
  } catch(e) {
    // error
  }
}

getMultiple

Fetches multiple key-value pairs for given array of keys in a batch. Once completed, invokes callback with errors (if any) and results.

Method:

static getMultiple(keys, [callback])

Return:

A Promise of array with coresponding key-value pairs found, stored as {key: value} array.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
keys Array Yes Array of key for the items to get.
callback ?(errors: ?Array, result: ?Array) => void No Function that will be called with a key-value array of the results, plus an array of any key-specific errors found.

Example:

getMultiple = async () => {
  let items

  try {
    items = await AsyncLocalStorage.getMultiple(['@key1', '@key2'])
  } catch(e) {
    // error
  }

  console.log(items)

  /*
    output: 
    [
      {key1: "hello1"},
      {key2: "hello2"}
    ]
  */
}

removeMultiple

Delete multiple key-value entries for given array of keys in a batch. Once completed, invokes a callback with errors (if any).

Method:

static removeMultiple(keys, [callback])

Return:

A Promise object.

Parameters:

NAME TYPE REQUIRED DESCRIPTION
keys Array Yes Array of key for the items to delete.
callback ?(errors: ?Array) => void No Function that will be called an array of any key-specific errors found.

Example:

removeFew = async () => {
  const keys = ['key1', 'key1']
  
  try {
    await AsyncLocalStorage.removeMultiple(keys)
  } catch(e) {
    // error
  }
}