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)
                 }
          })
 })
 

相关推荐

  1. uniapp异步api变成同步

    2023-12-31 15:36:03       58 阅读
  2. js的同步异步

    2023-12-31 15:36:03       41 阅读
  3. 异步同步

    2023-12-31 15:36:03       31 阅读
  4. 同步异步

    2023-12-31 15:36:03       36 阅读
  5. 007 异步同步

    2023-12-31 15:36:03       21 阅读
  6. 同步异步的区别?

    2023-12-31 15:36:03       48 阅读
  7. 异步复位同步释放原则

    2023-12-31 15:36:03       45 阅读
  8. ajax和同步异步

    2023-12-31 15:36:03       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-31 15:36:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-31 15:36:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-31 15:36:03       87 阅读
  4. Python语言-面向对象

    2023-12-31 15:36:03       96 阅读

热门阅读

  1. uniapp获取用户头像

    2023-12-31 15:36:03       85 阅读
  2. 【Qt-Qss-Style】

    2023-12-31 15:36:03       55 阅读
  3. 设计模式之装饰器模式

    2023-12-31 15:36:03       60 阅读
  4. IOC推导和本质

    2023-12-31 15:36:03       55 阅读
  5. 【leetcode】栈与队列总结

    2023-12-31 15:36:03       66 阅读
  6. 过滤、排序、分页、异常处理

    2023-12-31 15:36:03       51 阅读
  7. Python调用C++/C

    2023-12-31 15:36:03       59 阅读
  8. SpringBoot入门到精通-Spring Boot Jasypt Encrypt 演示

    2023-12-31 15:36:03       63 阅读
  9. 二叉树-遍历-重写

    2023-12-31 15:36:03       55 阅读
  10. 数据结构-怀化学院期末题(58)

    2023-12-31 15:36:03       61 阅读
  11. 多次运用集成函数处理蛋白质特征数据

    2023-12-31 15:36:03       60 阅读
  12. git教程——日常工作git使用流程

    2023-12-31 15:36:03       66 阅读
  13. Markdown“冷门”语法说明

    2023-12-31 15:36:03       67 阅读
  14. JPA 批量保存超级慢如何解决

    2023-12-31 15:36:03       61 阅读