Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/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_Async Await_Es6 Promise - Fatal编程技术网

Javascript 异步函数内部的新承诺,以获取异步函数外部的值

Javascript 异步函数内部的新承诺,以获取异步函数外部的值,javascript,async-await,es6-promise,Javascript,Async Await,Es6 Promise,我在一个异步函数中返回一个新的Promis,该函数正在向一个api发出请求,我可以在函数中通过控制台记录来自api的请求。但当我尝试解析同一行时,我安慰document.salled它不起作用。我希望checkPriceId变量返回一个承诺,然后我就可以抓住它。然后,但这似乎不起作用。我还尝试在documents.forEach循环中使用promise.all,但没有任何效果 任何帮助都将不胜感激 这是密码 const checkPriceId = async test => {

我在一个异步函数中返回一个新的Promis,该函数正在向一个api发出请求,我可以在函数中通过控制台记录来自api的请求。但当我尝试解析同一行时,我安慰document.salled它不起作用。我希望checkPriceId变量返回一个承诺,然后我就可以抓住它。然后,但这似乎不起作用。我还尝试在documents.forEach循环中使用promise.all,但没有任何效果

任何帮助都将不胜感激

这是密码

const checkPriceId = async test => {
      return new Promise((resolve, reject) => {
        const query = `*[_type == "products" && price_id == "${body.price_id}"]`
        client.fetch(query, {}).then(documents => {
          documents.forEach(document => {
            //console.log(document.sold)
            resolve(document.sold)
          })
        })
      })
    }

    checkPriceId.then(test => {
      console.log(test) // does nothing
    })

    console.log(checkPriceId) // just returns a async function

    checkPriceId()
??client.fetch已经返回了一个承诺,您也在一个异步函数中,该函数也返回了一个承诺。假设所有文档都有一个.sell,您正试图以数组形式返回:

const checkPriceId = async () => {
  const query = `*[_type == "products" && price_id == "${body.price_id}"]`
  const documents = await client.fetch(query, {})
  return documents.map(({ sold }) => sold)
}

checkPriceId.then((soldList) => {
  console.log(soldList)
})
这还会删除checkPriceId的测试参数,因为它没有被使用。

?client.fetch已经返回了一个承诺,您也在一个异步函数中,该函数也返回了一个承诺。假设所有文档都有一个.sell,您正试图以数组形式返回:

const checkPriceId = async () => {
  const query = `*[_type == "products" && price_id == "${body.price_id}"]`
  const documents = await client.fetch(query, {})
  return documents.map(({ sold }) => sold)
}

checkPriceId.then((soldList) => {
  console.log(soldList)
})

这也会删除checkPriceId的测试参数,因为它没有被使用。

作为@blex mentinoned,您没有在第13行调用checkPriceId

然而,正如@grodzi所提到的,您不能多次解决您的承诺。在第7行调用resolve fn后,将忽略后续调用

由于混合承诺和异步的方式可能会很冗长,我建议您在这里使用async/awiat。这将极大地提高代码的可读性

下面是一个固定的示例:

const checkPriceId = async (test) => {
  
  const query = `*[_type == "products" && price_id == "${body.price_id}"]`;

  const documents = await client.fetch(query, {}); // this could throw

  return documents.map(document => document.sold);
}

checkPriceId().then(test => {
  console.log(test) // this will log the return value from line 7
})

正如@blex mentinoned所说,您没有在第13行调用checkPriceId

然而,正如@grodzi所提到的,您不能多次解决您的承诺。在第7行调用resolve fn后,将忽略后续调用

由于混合承诺和异步的方式可能会很冗长,我建议您在这里使用async/awiat。这将极大地提高代码的可读性

下面是一个固定的示例:

const checkPriceId = async (test) => {
  
  const query = `*[_type == "products" && price_id == "${body.price_id}"]`;

  const documents = await client.fetch(query, {}); // this could throw

  return documents.map(document => document.sold);
}

checkPriceId().then(test => {
  console.log(test) // this will log the return value from line 7
})
您需要调用checkPriceId:checkPriceId。然后…您不想对每个文档调用resolve,您可能希望使用文档数组调用resolve.salled idem resolvedocuments.mapx=>x.salled,但首先您可以按照@blex指令调用checkPriceId:checkPriceId。然后…您不想对每个文档调用resolve顺便说一句,您可能希望使用一个documents数组调用resolve.salled idem resolvedocuments.mapx=>x.salled,但首先可以按照@blex指令进行操作