Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在另一个异步等待函数中使用函数结果_Javascript_Vue.js_Vuejs2_Nuxt.js - Fatal编程技术网

Javascript 如何在另一个异步等待函数中使用函数结果

Javascript 如何在另一个异步等待函数中使用函数结果,javascript,vue.js,vuejs2,nuxt.js,Javascript,Vue.js,Vuejs2,Nuxt.js,我有一个函数,如果另一个函数返回为true,则应该运行该函数: // in utils.js methods:{ funcOne(){ // do some thing return true } } //in component.vue methods:{ funcTwo(){ let x = this.funcOne() if(x){ // do something }else{ // do something

我有一个函数,如果另一个函数返回为true,则应该运行该函数:

// in utils.js
methods:{
  funcOne(){
    // do some thing
    return true
  }
}


//in component.vue
methods:{
  funcTwo(){
    let x = this.funcOne()
    if(x){
    // do something
    }else{
    // do something
    }
  }
}

我该怎么做?由于js是运行时,它不会等待
funcOne()
的结果,我知道我应该使用
Promise
async/await
。但是我不知道怎么做

更新

我按照建议做了,但仍然没有工作,所以我要解释一下情况: 我正在使用
甜蜜警报
。如果我的
甜蜜警报
被确认,则应发送axios请求。代码如下:

js是一个全局添加的mixin

async function sweetAlert(options) {
  // options: method('callback') , icon, title, text, callback, cValue
  if (options.method === 'confirm' || options.method === 'callback') {
    this.$swal({
      icon: options.icon,
      title: options.title,
      text: options.text,
      showCancelButton: true,
      cancelButtonText: this.lang.cancel,
      confirmButtonText: this.lang.confirm
    }).then(async (result) => {
      if (result.value) {
        if (options.method === 'callback') {
          let res = await options.callback(options.cValue)
          return res
        }

        return true
      }
    })
  }
}
我的组件脚本:

let res = await this.sweetAlert({
  method: ALERT_METHOD[2], // it is 'confirm'
  icon: ALERT_TYPE[2], // it is warning
  title: this.lang.deactivate, // added globally
  text: this.lang.deactivate_warning, // added globally
})

if (res) {
  this.activeSwitchDis = true // in my data
  this.activeChanged = true // in my data
  // this.axiosGet is a globally adde this.axios.get
  let response = await this.axiosGet(`product/deactive/${this.editProduct.pId}`)
  // this.resOk is globally added and check for 200
  if (this.resOk(response.status)) {
    // these are my data and funcs no prob with theme
    this.changeError(false)
    this.active = false
    this.activeChanged = false
    this.activeSwitchDis = false
    this.setEditProductActive(this.active)
  } else {
    // these are my data and funcs no prob with theme
    this.active = true
    this.activeChanged = false
    this.activeSwitchDis = false
  }
}
问题只是,如果我确认sweetAlert,axios必须运行

methods:{
  async funcOne(){
    // do some thing
    await someAsyncFunctionOrLogic();
    return true
  }
}


//in component.vue
methods:{
  async funcTwo(){
    let x = await this.funcOne()
    if(x){
    // do something
    }else{
    // do something
    }
  }
}
由于js是运行时,它不会等待
funcOne()的结果

由于示例代码中的
funcOne()
不是异步函数,因此这是不正确的:调用将等待函数完成并返回其值

我该怎么做?[…]我知道我应该使用Promise或
async/await
。但是我不知道怎么做

那么你最好继续读下去,因为你需要对它们有一个正确的理解才能有效地使用它

更新 现在来看看您的实际代码:
sweetAlert()
的实现实际上没有返回任何内容,因为
return
s的作用域是另一个函数:

#缩写代码:
异步函数sweetAlert(选项){
如果(…){
这个.swal({…}).then(异步(结果)=>{
如果(…){
let res=等待options.callback(options.cValue)
返回res
}
返回真值
})
}
}
因此
return res
return true
的作用域实际上是传递给
then()
处理程序的函数。此链将返回另一个承诺,该承诺将使用该
return
s的值进行解析。要将其捕获为
sweetAlert()
的返回值,您需要
返回它:

#缩写代码:
功能提示(选项){
如果(…){
//在此处为sweetAlert()返回链的结果
返回此值。$swal({…})。然后(异步(结果)=>{
如果(…){
let res=等待options.callback(options.cValue)
返回res
}
返回真值
})
}
}
请注意,
sweetAlert()
只有在进入第一个
if
块时才会返回内容。还要注意,在
sweetAlert()
函数中(但仅在其中的其他函数中)不使用
wait
,而是返回
Promise
而不是原始值,您可以省略
async
关键字

或者,您也可以使用
async/await

异步函数sweetAlert(选项){ 如果(options.method=='confirm'| | options.method==='callback'){ //在此等待返回值 让result=等待此操作。$swal({…}) if(result.value){ 如果(options.method==='callback'){ let res=等待options.callback(options.cValue) 返回res } //现在将从sweetAlert()返回 返回真值 } } }
funcOne不是一个异步函数,它似乎没有返回承诺。我按照你说的做了,但没有结果,所以用我的实际代码进行了更新。plz帮助我按照要求做了,但没有结果,所以用实际代码更新了我的问题。plz帮助