让同步函数同步执行,异步函数异步执行,并且让它们具有统一的 API

这里说两种方式,和官网的上的是一样的:

第一种使用: async+ 立即执行函数

    <div>方法一:async ()() 立即执行函数: {{doAsyncData.desc}}</div>

    doAsync () {
      const f = () => {
        return 'my '
      }
      // f()是同步函数的情况
      // (async () => f())();

      // f()是异步函数情况
      (async () => f())()
        .then((res) => {
          this.doAsyncData.desc = res + 'name is mayouchen'
        }).catch((err) => {
          console.log(err)
        })

      console.log('doAsync...next')
    }

第二种使用: 使用promise处理

    <div>方法二:promise的实现:{{doAsyncData.desc2}}</div>
    
    doPromise2 () {
       //(1)使用普通的promise方式
      const f = () => {
        return 'you '
      }
      (() => new Promise(
        resolve => resolve(f())
      ))()
      console.log('doAsync...after')

      // (2)使用Promise.try的方式
      let promise = Promise.try(() => {
        // 调用接口请求
        return 'my name is '
      })
      promise.then((res) => {
        this.doAsyncData.desc2 = `${res} Liming`
      }).catch(err => {
        console.log(err)
      })
    }

相关文章

最后的控制台返回空数组.控制台在ids.map函数完成之前运行va...
我正在尝试将rxJava与我已经知道的内容联系起来,特别是来自J...
config.jsconstconfig={base_url_api:"https://douban....
我正在阅读MDN中的javascript,并且遇到了这个谈论承诺并且不...
config.jsconstconfig={base_url_api:"https://douban....
这是我的代码main.cpp:#include<string>#include<...