util.AsyncStorage

Extends util.Storage.

Storage that provides asynchronous access to key values.

Constructor | Methods

Constructor

util.AsyncStorage()

Methods

Name

Returns

Description

add(key, object)

util.Storage

Adds an object to storage.

Inherited from util.Storage.

define(key[, depends, resolveCallback[, context]])

util.AsyncStorage

Defines an asynchronous value in storage.

get(key)

Object

Returns object stored for the specified key, or the primary key, if this is not a string.

Inherited from util.Storage.

isDefined(key)

Boolean

Checking if the key can be accessed in the storage.

remove(key)

util.Storage

Deletes the "key: value" pair from storage.

Inherited from util.Storage.

require(keys[, successCallback[, errorCallback[, context]]])

vow.Promise

Async request to get values from the storage.

Methods details

define

{util.AsyncStorage} define(key[, depends, resolveCallback[, context]])

Defines an asynchronous value in storage.

Returns self-reference.

Parameters:

Parameter

Default value

Description

key*

Type: String

The key to use for making an async call.

depends

Type: String[]

Array of keys from the current storage that must be initialized before this key. This argument can be omitted.

resolveCallback*

Type: Function

Function that defines the value accessed by the key. The first argument in resolveCallback is the provide function to pass the value to. Invocation of the provide function can be deferred. The following arguments are values from storage that are specified in dependencies. The order of modules follows their order in the depends array.

context

Type: Object

Execution context for the function.

* Mandatory parameter/option.

Examples:

1.

asyncStorage
    .define('red', function (provide) {
        provide('#FF0000');
     });

2.

asyncStorage
    .define('green', function (provide) {
        // The provide function can be called asynchronously.
        setTimeout(function () {
            provide('#008000');
        }, 400);
    });

3.

asyncStorage
    .define('yellow', function (provide) {
         provide('#FFFF00');
    })
    // To define the 'violet' key value, the 'yellow' key value is required.
    .define('violet', ['yellow'], function (provide, yellow) {
        console.log(yellow); // #FFFF00
        setTimeout(function () {
            provide('#9B30FF');
        }, 400);
    });

4.

var asyncStorage = new ymaps.util.AsyncStorage();
asyncStorage
    .define('red', function (provide) {
        provide('#FF0000');
     })
    .define('green', function (provide) {
        setTimeout(function () {
            provide('#008000');
        }, 400);
    })
    .define('yellow', function (provide) {
         provide('#FFFF00');
    })
    .define('violet', ['yellow'], function (provide, yellow) {
        setTimeout(function () {
            provide('#9B30FF');
        }, 400);
    });
// Requesting
asyncStorage.require(['red', 'green', 'violet'])
    .spread(function (red, green, violet) {
         // Outputs #FF0000, #008000, #9B30FF.
         console.log(red, green, violet);
         // After the first async access, values can be accessed from the synchronous interface.
         // Outputs #FF0000 #008000.
         console.log(asyncStorage.get('red'), asyncStorage.get('green'), asyncStorage.get('violet'));
         // The value for the 'yellow' key is also in the storage now, because it was required in order to define 'violet'.
         // Outputs #FFFF00.
         console.log(asyncStorage.get('yellow'));
    });

isDefined

{Boolean} isDefined(key)

Checking if the key can be accessed in the storage.

Returns true - defined, false - not.

Parameters:

Parameter

Default value

Description

key*

Type: String

Key for the value.

* Mandatory parameter/option.

Example:

if (asyncStorage.isDefined('red')) {
    asyncStorage.require('red')
        .spread(function (red) {
            // ...
        });
}

require

{vow.Promise} require(keys[, successCallback[, errorCallback[, context]]])

Async request to get values from the storage.

Returns Promise that represents async access to the value.

Parameters:

Parameter

Default value

Description

keys*

Type: String|String[]

Key or array of keys.

successCallback

Type: Function

Callback to invoke after getting all the values. Values from storage are passed to the function as arguments. The arguments are ordered according to their order in the keys array.

errorCallback

Type: Function

Callback to use if an error occurs. The error object is passed to the function.

context

Type: Object

Callback execution context.

* Mandatory parameter/option.

Examples:

1.

asyncStorage.require(['green'])
    .spread(function (green) {
        // ...
    });

2.

var asyncStorage = new ymaps.util.AsyncStorage();
asyncStorage
    .define('red', function (provide) {
        provide('#FF0000');
     })
    .define('green', function (provide) {
        setTimeout(function () {
            provide('#008000');
        }, 400);
    });
// Requesting
asyncStorage.require(['red', 'green'])
    .spread(function (red, green) {
         // Outputs #FF0000 #008000.
         console.log(red, green);
    });