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

mac2022-06-30  25

// 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

最新回复(0)