把函数改写为Promise
JS
var 选择函数 = function(str,success,faild){
if(confirm(str)){
if(typeof success == 'function'){
success();
}
}else{
if(typeof faild == 'function'){
faild();
}
}
}
选择函数("是否确认?",()=>{alert("是")},()=>{alert("否")});
var 选择函数Promise = function(str){
return new Promise(function(resolve) {
if(confirm(str)){
resolve();
}else{
rejected();
}
})
}
选择函数Promise("是否确认?").then(()=>{alert("是")}).catch(()=>{alert("否")})
解释
1、把判断function的函数去掉
2、return new Promise(function(resolve)
3、resolve(); 、rejected();
4、then()、catch()