惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - jowo

websql操作类封装 - jowo async/await 来处理异步详解 使用富文本编辑器wangEditor3 Promise的基本用法 - jowo zTree setting 配置 WebSQL vue-cli快速构建项目 接收的第一个参数是模块的局部状态对象。 如何使用JavaScript检测Ctrl+V,Ctrl+C? 什么情况下我应该使用 Vuex? Vue+node.js实现一个简洁的个人博客系统 Vuex 是什么? vue中mixins的理解及应用 Vue:slot用法 - jowo web.xml文件的的param-name Robots协议一定放在网站根目录下 浅谈重写与重载 笔记纪要:C# WebService URL重写 Java应用监控利器JMX
Vue.js中Promise、异步、同步、定时器
jowo · 2020-05-27 · via 博客园 - jowo

Posted on 2020-05-27 16:42  jowo  阅读(1134)  评论()    收藏  举报

Vue.js使用Axios或Ajax请求后台时,是异步请求,所有的请求同时执行,后面的then随机执行

要想实现同步执行可以使用await和async

在需要同步执行(即后面一个方法的参数是前一个方法的返回值),在父层方法前面加async,并且在字层方法前面加await,

然后方法中需返回Promise对象,用resolve()表示执行成功,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

async mounted() {

    this.currentTime = parseTime(

      new Date().toString(),

      '{y}-{m}-{d} {h}:{i}:{s}'

    )

    await this.getOrgs()

    await this.getQuDao()

    await this.getProductList()

    await this.queryData()

  },

getOrgs() {

  return newPromise((resolve,reject) => {

     ...

     resolve(true)

  }) 

}

getQuDao() {

  return newPromise((resolve,reject) => {

     ...

     resolve(true)

  }) 

}

...

当需要等到所有的请求都成功完成请求数据,再做其他操作时可以用到Promise.All方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

initPage() {

  const _amt = this.getData()

  ...

  Promise.all([

        _amt,_amtPie,

        _amtRepay,_repayAmtPie,

        _num,_intoPie,

        _rate,_rateBar,

        _map,_table

      ]).then(([_amt, _amtPie, _amtRepay, _repayAmtPie, _num, _intoPie, _rate,

  _rateBar, _map, _table]) => {

      ...

      })

      .catch(() => {

       ...

      })

}

getData() {

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

    gethttpData({}).then(response => {

       ... 

     }).catch(err => {

       ...

      })

  }) 

}

定时器(1)

setTimeout只在指定时间后执行一次,代码如下:

1

2

3

4

5

6

7

8

9

10

<script> 

function hello(){ 

alert("hello"); 

var t1 = window.setTimeout(hello,1000); 

var t2 = window.setTimeout("hello()",3000);

window.clearTimeout(t1);

</script>  

定时器(2)

setInterval以指定时间为周期循环执行,代码如下:

1

2

3

4

5

6

setInterval('refreshQuery()',8000);  

function refreshQuery(){ 

   $("#mainTable").datagrid('reload',null);