async function say() { console.log(3); await new Promise((resolve, reject) => { // setTimeout(() => { console.log(1); resolve() // }) }) console.log(2); } say() console.log(5);解释一下这个代码的执行顺序
时间: 2024-02-14 16:27:35 浏览: 26
这段代码的执行顺序如下:
1. 首先,调用了`say()`函数。
2. 在`say()`函数内部,首先打印出了数字3。
3. 接着,创建了一个新的Promise对象,并使用`await`关键字等待Promise对象的状态变为resolved。
4. 在Promise的构造函数中,打印出了数字1,然后调用了`resolve()`方法来将Promise的状态变为resolved。
5. 因为使用了`await`关键字,所以在这里会暂停代码的执行,等待Promise对象的状态变为resolved。
6. 在这个等待的过程中,控制权返回到了`say()`函数的调用点,在这里打印出了数字5。
7. 当Promise对象的状态变为resolved时,代码继续执行,打印出了数字2。
8. 整个代码执行完毕。
所以,最终的输出结果是3、5、1、2。
相关问题
async function say() { console.log(3); await new Promise((resolve, reject) => { setTimeout(() => { console.log(1); resolve() }) }).then(() => { console.log(4); }) console.log(2); } say() console.log(5);
这段代码的执行结果是:
3
1
4
2
5
首先,函数`say`被调用,打印出3。然后,`await`关键字暂停了代码的执行,直到`Promise`对象的状态变为resolved。在这个`Promise`中,通过`setTimeout`函数设置了一个延迟,1秒后打印出1,并且调用`resolve`方法将`Promise`状态变为resolved。接着,通过`.then()`方法注册的回调函数打印出4。
然后,`await`关键字继续执行剩下的代码,打印出2。最后,在`say()`函数外部,打印出5。
修改这段代码,使第一个定时任务先执行,第一个定时任务执行完之后,开始执行第二个定时任务,并且第二个定时任务执行时,第一个定时任务不再开启。 findBlue(){ var _this = this; wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, interval: 0, success:(res) => { console.log("搜索蓝牙设备成功...") setTimeout(function(){ _this.getBlue() },500) }, fail: (res) => { console.log('搜索附近的蓝牙设备失败') console.log(res) } }) }, // 3.搜索蓝牙设备之后,需要获取搜索到的蓝牙设备信息 getBlue(){ var _this = this; wx.getBluetoothDevices({ success: (res) => { console.log(res) if(res.devices.length > 0){ for (var i = 0; i < res.devices.length; i++){ // 判断里面是否有我想要的蓝牙设备 if (res.devices[i].name === this.data.inputValue || res.devices[i].localName === this.data.inputValue){ console.log("name:"+res.devices[i].name) this.setData({ isFound: true, deviceId: res.devices[i].deviceId }) //扫描到目标设备之后关闭蓝牙搜索 wx.stopBluetoothDevicesDiscovery({ success: (res) => { console.log('扫描到目标设备之后关闭蓝牙搜索'); setTimeout(function(){ _this.connetBlue(_this.data.deviceId);//4.0 },500) } }) // this.connetBlue(res.devices[i].deviceId);//4.0 return; } } } console.log("重新找....") var time = setTimeout(function(){ if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("执行任务1==================") _this.findBlue() } },500) setTimeout(function(){ console.log("执行任务2---------------------") clearTimeout(time) console.log("清除time定时任务") console.log("isFound:",_this.data.isFound) console.log("isTui:",_this.data.isTui) console.log("isTip:",_this.data.isTip) if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("重新打开适配器") wx.openBluetoothAdapter({ success: (res) => { console.log('初始化蓝牙设备成功') //获取适配器状态 wx.getBluetoothAdapterState({ success(res) { setTimeout(function(){ _this.findBlue();//2.0 },800) } }) } }) } }, 3000) }, fail: () =>{ console.log("搜索蓝牙设备失败") } }) },
修改后的代码如下,使用Promise和async/await实现任务的异步执行:
```
async function findAndConnectBlue() {
try {
// 1. 先执行任务1
await searchBlue();
// 2. 任务1执行完后,执行任务2
await connectBlue();
} catch (error) {
console.log(error);
}
}
function searchBlue() {
return new Promise((resolve, reject) => {
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
interval: 0,
success: (res) => {
console.log("搜索蓝牙设备成功...");
wx.getBluetoothDevices({
success: (res) => {
console.log(res);
if (res.devices.length > 0) {
for (var i = 0; i < res.devices.length; i++) {
// 判断里面是否有我想要的蓝牙设备
if (
res.devices[i].name === this.data.inputValue ||
res.devices[i].localName === this.data.inputValue
) {
console.log("name:" + res.devices[i].name);
this.setData({
isFound: true,
deviceId: res.devices[i].deviceId,
});
//扫描到目标设备之后关闭蓝牙搜索
wx.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log("扫描到目标设备之后关闭蓝牙搜索");
resolve();
},
});
return;
}
}
}
console.log("重新找....");
setTimeout(() => {
if (
this.data.isFound == false &&
this.data.isTui == false &&
this.data.isTip == false
) {
console.log("执行任务1==================");
searchBlue().then(() => resolve());
} else {
resolve();
}
}, 500);
},
fail: () => {
console.log("搜索蓝牙设备失败");
reject();
},
});
},
fail: (res) => {
console.log("搜索附近的蓝牙设备失败");
console.log(res);
reject();
},
});
});
}
function connectBlue() {
return new Promise((resolve, reject) => {
console.log("执行任务2---------------------");
console.log("清除time定时任务");
console.log(
"isFound:",
this.data.isFound,
"isTui:",
this.data.isTui,
"isTip:",
this.data.isTip
);
if (
this.data.isFound == false &&
this.data.isTui == false &&
this.data.isTip == false
) {
console.log("重新打开适配器");
wx.openBluetoothAdapter({
success: (res) => {
console.log("初始化蓝牙设备成功");
//获取适配器状态
wx.getBluetoothAdapterState({
success: (res) => {
setTimeout(() => {
searchBlue().then(() => resolve());
}, 800);
},
});
},
});
} else {
resolve();
}
});
}
findAndConnectBlue();
```