Javascript 如何等待异步函数并收集响应

Javascript 如何等待异步函数并收集响应,javascript,node.js,asynchronous,es6-promise,Javascript,Node.js,Asynchronous,Es6 Promise,直到现在,我还以为wait使我的程序同步。然而,我看到await只等待async函数通过承诺得到解决,然后整个程序继续运行。那么,等待和收集异步函数响应的正确方法是什么 原始代码: let结果={ 名称:“”, 国家:'' }; const data=wait query.getCachedData(did); result.name=data.name;//未定义 result.country=data.country//未定义 控制台日志(结果); 我不知道为什么,但等待异步函数结果是有效

直到现在,我还以为
wait
使我的程序同步。然而,我看到
await
只等待
async
函数通过承诺得到解决,然后整个程序继续运行。那么,等待和收集异步函数响应的正确方法是什么

原始代码:

let结果={
名称:“”,
国家:''
};
const data=wait query.getCachedData(did);
result.name=data.name;//未定义
result.country=data.country//未定义
控制台日志(结果);
我不知道为什么,但等待异步函数结果是有效的:

let结果={
名称:“”,
国家:''
};
const data=wait query.getCachedData(did);
result.name=wait data.name;/'乔恩的
result.country=等待数据。country;/'美国的
控制台日志(结果);
但我不确定这是否是解决办法

由于
getCachedData
返回承诺,我认为这可能是正确的方法,但是
then()
/
catch()
没有执行

query.getCachedData(did).then((tfnda)=>{
result.name=data.name;
result.country=data.country;
控制台日志(结果);
}).catch((dbError)=>{
log(dbError);
});

有人能纠正我以正确的方式获得
结果吗?

您正确的说法是
wait
等待
async
函数返回承诺。对于您的代码示例,我建议如下:

let result={
    name:'',
    country:''
};

async function getData() {
    const data = await query.getCachedData(did);
    result.name = data.name; // 'Jon'
    result.country = data.country; // 'US'
    console.log(result);
}

wait
只能在
async
函数中使用。它将暂停函数,直到从
query.getCachedData()
收到承诺,然后将该响应保存到
常量数据中,该数据可用于设置
结果
对象的名称和国家/地区。您还可以查看MDN文档中的和。

await
不会使您的异步代码同步,并且没有任何合理的理由这样做。。。在幕后,它会返回一个承诺,并将其与当时联系在一起,而不是你必须与当时的自己联系在一起

您使用then关键字所做的就是
wait
为您所做的

您可以使用适合自己的任何东西,但是
async/await
使您的代码更易于阅读

如果这行得通的话

result.name = await data.name; 
这意味着名称是一个异步getter函数,您必须等待它获得结果。你也可以这样做
data.name.then(name=>doWhateverYouWantWithName(name))
或像您已经做的那样使用
wait
关键字,这应该更好。

一个承诺是异步函数的返回。结果可能还没有完成。 这就是为什么你可以等待一个方法(就像你做的那样)。这将在计算完成时设置函数的返回值

或者你可以使用“then”:

const data1 = await query.getCachedData(did);
//data1 has a value at this point of code

const data2;
query.getChachedData(did).then((result)=>{data2 = result});
//data2 can have a value or will get one later (this is async) at this point of code
通过Promise all,您可以让多个方法异步运行并同时等待所有方法。

直到现在,我还以为wait使我的程序同步

Async/await
使代码看起来像是同步的,但后面只是Javascript中承诺的语法糖真的吗?

只是
返回promise()。然后(result=>result)

我看到wait只会等待异步函数被解析,然后整个程序继续运行

当您使用承诺时,它们不会使Node.js代码同步运行,另一方面,承诺允许您编写看起来同步的流

那么,等待和收集异步函数响应的正确方法是什么

根据您的示例,代码如下所示:

const queryResult = did => query.getCachedData(did).then(tfnData => tfnData);

// Without async/await    
queryResult(did)
  .then(data => {
    const { name, country } = data;
    const result = { name, country };
    console.log(`${result.name}, ${result.country}`); // Jon, US
  })
  .catch(error => console.log(`Error produced: ${error}`));

// With async/await
(async () => {
  try {
    // ... Some other code ....
    // ... Some other code ....
    // ... Some other code ....

    const data = await queryResult(did);
    const { name, country } = data;
    const result = { name, country };
    console.log(`${result.name}, ${result.country}`); // Jon, US
  } catch (error) {
    console.log(`Error inside try-catch: ${error}`);
  }
})();

我将重申罗伯特的评论,因为我认为他没有足够的代表权来发表真正的评论。请提供
getCachedData
的来源或提供足够的信息以重现此问题。您使用的数据库库是什么?我们怎么能让这一切发生在我们这边?请阅读。query.getCachedData是异步方法吗?你能告诉我们这个方法是什么样的吗?我怀疑这是一个异步方法,或者它会返回一个承诺,因为您不能等待它,也不能执行then/catch
const queryResult = did => query.getCachedData(did).then(tfnData => tfnData);

// Without async/await    
queryResult(did)
  .then(data => {
    const { name, country } = data;
    const result = { name, country };
    console.log(`${result.name}, ${result.country}`); // Jon, US
  })
  .catch(error => console.log(`Error produced: ${error}`));

// With async/await
(async () => {
  try {
    // ... Some other code ....
    // ... Some other code ....
    // ... Some other code ....

    const data = await queryResult(did);
    const { name, country } = data;
    const result = { name, country };
    console.log(`${result.name}, ${result.country}`); // Jon, US
  } catch (error) {
    console.log(`Error inside try-catch: ${error}`);
  }
})();