js new Promise的基本用法
function easyShare(config) {return new Promise((resolve, reject) => {try {if (config === true) {console.log(11, config)setTimeout(() => {resolve(‘成功了’)}, 1000)} else {// throw errorthrow ‘失败了’}}
·
function easyShare(config) {
return new Promise((resolve, reject) => {
try {
if (config === true) {
console.log(11, config)
setTimeout(() => {
resolve(‘成功了’)
}, 1000)
} else {
// throw error
throw ‘失败了’
}
} catch (error) {
console.log(12, error)
reject(error);
}
});
}
1.成功的情况
easyShare(true).then(
(data) => {
//接收成功的消息
console.log(13, data)
},
(data) => {
console.log(14, data)
}
)
2.
失败的情况
easyShare(false).then(
(data) => {
console.log(13, data)
},
(data) => {
//接收失败的消息
console.log(14, data)
}
)
更多推荐
已为社区贡献1条内容
所有评论(0)