浪漫的Promise

<script type="text/javascript">
            const myPromise = (girl) => {
                let sayLove = new Promise((resolve, reject) => {
                    console.log('你正在追求' + girl)
                    var res = '考虑中···'; //追求结果  
                    console.log(res)
                    setTimeout(function() {
                        var num = Math.ceil(Math.random() * 10);
                        // 随机生成一个1-10的随机数,模拟追求成功率
                        if (num <= 1) {
                            res = '钻戒够大!'
                            resolve(res)
                        } else {
                            res = '阿珍胃不太好!'
                            reject(res)
                        }
                    }, 3000)
                })
                return sayLove
            }
            myPromise('阿珍').then(function(res) {
                console.log('追求成功!原因是:' + res)
            }, function(err) {
                console.log('追求失败!原因是:' + err)
            })
</script>

明天就要面试了,复习一下Promise,希望可以顺利拿到offer,加油加油!!!

Last modification:August 12th, 2020 at 01:17 am
如果觉得我的文章对你有用,请随意赞赏