resolve promise with then 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(10)
.then(function (value) {
console.log(value);
return testFunction(50);
})
.then(function (value) {
console.log(value);
return testFunction(150);
})
.catch(function (error) {
console.log(`エラーです。${error}`);
})
Example 2: .then javascript
const promise2 = doSomething().then(successCallback, failureCallback);
Example 3: 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}`);
})