Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of return value after then in vue

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of the return value instance analysis after then in vue, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this vue then return value instance analysis article. Let's take a look at it.

Return value after then

Promise deals with asynchronous calls, which are non-blocking. When calling, you don't know when it ends, so you won't wait until he returns a valid data before proceeding to the next step.

We can use async and await to get our return value

Add async to the function in vue

Async del (id) {var that=this var params= {sensorCommonId:id} return DelSensorCommonInfo (params) .then (function (res) {return Promise.resolve (res.data.Data);});}

Add async to the function we call when calling the del function

Async more () {var index= await that.del (Array [I] .SensorCommonId) console.log (index)} function getSomething () {return "something";} async function testAsync () {return Promise.resolve ("hello async");} async function test () {const v1 = await getSomething (); const v2 = await testAsync (); console.log (v1, v2);} test (); get the return value in .then ()

Upload the file to Aliyun as an example:

Export function uploadObj ({file}, type) {let name = `pathname / ${Date.parse (new Date ()) + file.uid}`; / / define a unique file name const fileName = type = = 'excel'? Name + ".xlsx": name; const ContentType = type = = 'excel'? "text/xml": "image/jpeg"; new OSS (conf) .put (fileName, file, {ContentType: ContentType}). Then (({res, url}) = > {if (res & & res.status = = 200) {this.$message.success (upload successful); return url}). Catch (() = > {this.$message.error (upload failed);});}

The above code can upload the image / excel to the Ali cloud server. After the upload is successful, the Ali Cloud service will return a URL. At this point, if you directly return url, then the received url is undefined.

The solution is as follows: export function uploadObj ({file}, type, callback) {let name = `pathname / ${Date.parse (new Date ()) + file.uid}`; / / define a unique file name const fileName = type = = 'excel'? Name + ".xlsx": name; const ContentType = type = = 'excel'? "text/xml": "image/jpeg"; new OSS (conf) .put (fileName, file, {ContentType: ContentType}). Then (({res, url}) = > {if (res & & res.status = = 200) {this.$message.success (upload successful); callback (url)}). Catch (() = > {this.$message.error (upload failed);}) } call this method this.uploadObj ({file}, "excel", url = > this.importData (url))

The third argument passed in is the callback function, so that you can get the url directly in the importData method.

This is the end of the article on "instance Analysis of return values after then in vue". Thank you for reading! I believe you all have a certain understanding of the knowledge of "instance Analysis of return values after then in vue". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report