nakome
2/29/2016 - 9:30 PM

Test promise with console color for Success or Error

Test promise with console color for Success or Error

// the App fn
function App() {}
// prototype
App.prototype = {
    // create section  app.section(class,text);
    section: function(cls, txt) {
        // create element section
        var s = document.createElement('section');
        // add class
        s.className = cls;
        // add text
        s.textContent = txt;
        // create Promise
        return new Promise(function(resolve, reject) {
            //if text resolve
            if (txt) {
                document.body.appendChild(s);
                resolve('Success the section is created');
            } else {
                reject('Error the section is not created');
            }
        });
    },
    // notification in console app.notification('success',resolve);
    notification: function(type,info){
      if(type === 'success'){
        // Success
        console.log('%c' +info,
        'background: green; color: white;padding:5px 8px');
      }else if(type === 'error'){
        // Error
        console.log('%c' +info,
        'background: red; color: white;padding:5px 8px');
      }
    }
}

// start app
var app = new App(),
    // text example
    text = 'Hello World';

// create section with hello World
app.section('box',text )
.then(function(resolve) {
    app.notification('success',resolve);
}).catch(function(reject) {
    app.notification('error',reject);
});

// create section without text, get Error
app.section('box','' )
.then(function(resolve) {
    app.notification('success',resolve);
}).catch(function(reject) {
    app.notification('error',reject);
});