Promise 中处理的是异步调用,异步调用是非阻塞式的,在调用的时候并不知道它什么时候结束,也就不会等到他返回一个有效数据之后再进行下一步处理

可以使用 async 和 await来得到我们的返回值

 

 

 

在vue 中的函数加上async 

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

 

在我们调用所在的函数中也加上 async 在调用del函数时  

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();

 

参考  https://segmentfault.com/a/1190000007535316

 

Logo

前往低代码交流专区

更多推荐