util.AsyncStorage
Extends util.Storage.
Storage that provides asynchronous access to key values.
Constructor
util.AsyncStorage()
Methods
Name |
Returns |
Description |
Adds an object to storage. Inherited from util.Storage. |
||
define(key[, depends, resolveCallback[, context]]) |
Defines an asynchronous value in storage. |
|
Object |
Returns object stored for the specified key, or the primary key, if this is not a string. Inherited from util.Storage. |
|
Boolean |
Checking if the key can be accessed in the storage. |
|
Deletes the "key: value" pair from storage. Inherited from util.Storage. |
||
require(keys[, successCallback[, errorCallback[, context]]]) |
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 |
|
— |
Type: String The key to use for making an async call. |
— |
Type: String[] Array of keys from the current storage that must be initialized before this key. This argument can be omitted. |
|
— |
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. |
|
— |
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 |
|
— |
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 |
|
— |
Type: String|String[] Key or array of keys. |
— |
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. |
|
— |
Type: Function Callback to use if an error occurs. The error object is passed to the function. |
|
— |
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);
});
Mandatory parameter/option.