令人窒息的操作,nodejs面向对象。


// async函数返回一个 Promise 对象,可以使用then方法添加回调函数。
// 当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句。
class Demo {
    //构造函数
    constructor(x, y) {
        this.x = x //类中变量
        this.y = y
    }

    add = () => { //普通函数返回x+y的和
        return this.x + this.y
    }
    sleep = time => {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve('ok')
            }, time);

        })
    }
    //async 用于申明一个 function 是异步的,所以在该function里面的程序都是异步的
    start = async () => {
        let result = await this.sleep(1000)//await后面可以接着一个直接变量或者是一个promise对象
        let sum = this.add()
        console.log("了解", sum)
        return result
    }
}

let demo = new Demo(2, 3)
demo.start().then(m => {
        console.log(m);
    }
);

转载于:https://www.cnblogs.com/c-x-a/p/11248423.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值