Request and Error Handling

Promise

Socketize.js implements Promise, a relatively new async operation handler. With every request you get a promise object. If you have no idea what promise is you can read this article.

Socketize does not force you to use everything of the promise implementation.

This how you send requests:

socketize.get('full_name').then(function(fullName) {
    // Get full_name success
    socketize.set(fullName + '.attributes', {height: '6ft'});
});

This is how you send multiple requests, one after one:

socketize.get('full_name').then(function(fullName) {
    // Get full_name success
    return socketize.set(fullName + '.attributes', {height: '6ft'});
}).then(function() {
    // Set fullName + '.attributes' success
    return socketize.set('updated_at', new Date());
}).then(function() {
    // Set updated_at success
    alert('All done!');
});

Response

Response is always passed with a promise(callback). Whenever you interact with Socketize you will get a response back. For example:

socketize.get('full_name').then(function(response) {
    console.log(response); // the full_name
});

In response you will get the full_name you stored previously, null if no full_name is stored.

Error Handling

It's easy. Just pass a second callback in the then(successCallback, errorCallback)

socketize.get('full_name').then(
    function(fullName) {
        // Got the full_name, no error
    } ,
    function(error) {
        alert(error.message);
    }
);

Rather than catching error in each step, you can also catch all at once.

socketize.get('full_name').then(function(fullName) {
    // Get full_name success
    return socketize.set(fullName + '.attributes', {height: '6ft'});
}).then(function() {
    // Set fullName + '.attributes' success
    return socketize.set('updated_at', new Date());
}).then(function() {
    // Set updated_at success
    alert('All done!');
}).catch(function(error) {
    // Error in of the steps above
    alert(error.message);
});