uniapp将异步api变成同步

<template>

<view class="content">

<image class="logo" src="/static/logo.png"></image>

<view class="text-area">

<text class="title">{{title}}</text>

</view>

</view>

</template>

<script>

import {

request

} from '@/tools/request/index.js'

export default {

data() {

return {

title: 'Hello',

}

},

onShow() {

this.fun1()

},

methods: {

async fun1() {

//等待执行

const rs = await this.fun2()

console.log(rs);

console.log('执行完成')

},

//耗时操作

fun2() {

return new Promise((resolve,reject) => {

uni.request({

url:'http://www.baidu.com',

success: (res) => {

resolve('ok')

},

fail: (err) => {

reject('err')

}

})

})

}

}

}

</script>

<style>

.content {

display: flex;

flex-direction: column;

align-items: center;

justify-content: center;

}

.logo {

height: 200rpx;

width: 200rpx;

margin-top: 200rpx;

margin-left: auto;

margin-right: auto;

margin-bottom: 50rpx;

}

.text-area {

display: flex;

justify-content: center;

}

.title {

font-size: 36rpx;

color: #8f8f94;

}

.red {

color: red;

}

.blue {

color: blue;

}

</style>

如果是showModal

示例

return new Promise( async resolve=> {

uni.showModal({

title:'title',

content:'content',

success: (res) => {

resolve(res)

}

})

})

相关推荐
COOLMO研究AI3 分钟前
Next.js App Router 中 sitemap、robots 与 canonical 的配置与排查
前端·web·js·网站优化
DyLatte26 分钟前
AI 给了我 8 个优化方案,全都是对的,但没有一个有用
前端·后端·程序员
AI情绪识别开源30 分钟前
检信ALLEMOTION 认知评估分析器 · WebSocket 推送数据文档
javascript
OpsEye35 分钟前
公司内部全员使用 AI,如何保证会话合规、行为可审计?
javascript·ai编程
the局外人35 分钟前
一座不够大的城市,装下了我毕业后的成长
前端·程序员·求职
涛涛ing1 小时前
周下载量1.1亿的Tailwind,为什么养不活自己?
前端
变与不变8061 小时前
JS事件机制精讲
前端·javascript
装备研究社1 小时前
Promise 的五个状态,90% 的人只说对了三个
前端
小聪7081 小时前
elpis-core 抽离 npm 包过程的难点和卡点
前端·架构
钱栈up2 小时前
坯料管理导入功能bug修复:只改前端一个文件就解决问题
前端·bug