new promise without reject code example

Example 1: promise resolve reject

function testFunction(value) {
    return new Promise(function (resoleve, reject) {
        setTimeout(function () {
            let n = value;
            if(n<100){
                resoleve("範囲内");
            }else{
                reject("範囲外");
            }
        }, 2000);
    })
}
 
testFunction(1000)
    .then(function (value) {
        console.log(value);
    })
    .catch(function(error){
        console.log(`エラーです。${error}`);
    })

Example 2: promises and not callbacks

var Button = function(content) {   this.content = content;};Button.prototype.click = function() {  console.log(this.content + ' clicked');}var myButton = new Button('OK');myButton.click();var looseClick = myButton.click;looseClick(); // not bound, 'this' is not myButton - it is the global objectvar boundClick = myButton.click.bind(myButton);boundClick(); // bound, 'this' is myButton

Tags:

Misc Example