how to resolve promise and return value code example

Example 1: promise resolve reject

function testFunction() {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve('hello!');
        }, 1000);
    })
}
 
testFunction()
    .then(function (value) {
        console.log(value);
    })
    .catch(function (error) {
        console.log(error);
    })

Example 2: 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}`);
    })

Tags:

Misc Example