我有一个与放置向导 REST API 交互的 React 应用程序。在我的代码中,我注意到以下奇怪的错误。
在尝试设置两个变量的值,然后使用它们来设置状态时,我有以下内容
const [inventorRes, sellerRes] =
await Promise.all([
LoginTransport.getAllUsers('Inventory Owner'),
LoginTransport.getAllUsers('Seller'),
]);
其中LoginTransport是调用外部API的方法。现在我注意到,在执行此操作时,inventor res 和 sellerRes 都将设置为 LoginTransport.getAllUsers('Inventory Owner') 的值,而不是各自的参数。
当我把它分解成:
const inventorRes = await LoginTransport.getAllUsers('Inventory Owner');
const sellerRes = await LoginTransport.getAllUsers('Seller');
行为是正确的。
我的理解来自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all Promise.all 返回了一个可迭代对象。 javascript 是否会尝试将左侧可迭代对象的值与右侧可迭代对象的值进行匹配?假设这是真的,但我坚持使用 Promise.all() 我怎样才能完成我想做的任务?
最佳答案
Now I noticed that in doing this, both inventor res and sellerRes would be set to the value of
LoginTransport.getAllUsers('Inventory Owner')
as opposed to the respective arguments.
这可能是 LoginTransport.getAllUsers
的错误。 Promise.all
does你期望它做什么。
关于javascript - Node.js 的故障 promise 复制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42196767/