These are all problems that our last company encountered. Ah Wei asked me and solved them today. There is a demand cycle request an interface for data (this is supposed to do the background, cycle request does not conform to the specifications), but is met, the problem is the loop request but interface is asynchronous, you returned passed in order to does not have returned in order to, at that time, I thought a lot of ways, theory is feasible synchronous request interface, But I can’t try because change is too big, then there is await method, but because the lard tried not make the heart, for a moment I would say, why not make request to the last time 200 milliseconds, while also missed realized, but a set of down more slowly, sometimes the sequence network or computer slow reaction can also lead to confusion.

Start by Posting a picture of what you tried before

Because I failed to use async in this way, and I didn’t understand async thoroughly. I just waited for time-consuming requests with await and ignored async asynchrony, so the for loop was not blocked. Then I looked for ways to block the for loop everywhere, but there were many online instructions on how to teach asynchrony, and few on how to block the program. It came to nothing.

And then it finally worked out

mounted() {
				this.queryNewFund()
			},
			methods: {
				async queryNewFund() {
					let that = this
					let codeList = ['1', '2', '3']
					for (let i = 0; i < codeList.length; i++) {

						let obj = await that.getData(codeList[i]);

						console.log(obj)
					}
				},
				getData(code) {
					if (code == '1') {
						return new Promise((resolve) => {
							setTimeout(() => {
								resolve('aaa')
							}, 2000)
						})
					}
					if (code == '2') {
						return new Promise((resolve) => {
							setTimeout(() => {
								resolve('bbb')
							}, 500)
						})
					}
					if (code == '3') {
						return new Promise((resolve) => {
							setTimeout(() => {
								resolve('ccc')
							}, 100)
						})
					}
				}
			}
Copy the code

The printing order aaa, BBB, CCC solves the business requirement, and the for loop is blocked.