Documentation
Reference
2.1.55 (current version)
collection
interactivityModel
Interfaces

util.AsyncStorage

Extends util.Storage.

Storage that provides asynchronous access to key values.

Constructor | Methods

Constructor

util.AsyncStorage()

Methods

NameReturnsDescription
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

Defines an asynchronous value in storage.

Returns self-reference.

Parameters:

ParameterDefault valueDescription
key*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

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*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

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) {
         console.log(red, green, violet); // #FF0000 #008000 #9B30FF
         // After the first async access, values can be accessed from the synchronous interface.
         console.log(asyncStorage.get('red'), asyncStorage.get('green'), asyncStorage.get('violet')); // #FF0000 #008000
         // The value for the 'yellow' key is also in the storage now, because it was required in order to define 'violet'.
         console.log(asyncStorage.get('yellow')); // #FFFF00
    });

isDefined

{Boolean} isDefined(key)

Checking if the key can be accessed in the storage.

Returns true - defined, false - not.

Parameters:

ParameterDefault valueDescription
key*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: String

Key for the value.

* Mandatory parameter/option.

Example:

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

require

Async request to get values from the storage.

Returns Promise that represents async access to the value.

Parameters:

ParameterDefault valueDescription
keys*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

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) {
         console.log(red, green); // #FF0000 #008000
    });