我觉得我在这里忽略了一些东西,但我试图编写一个返回同步值的函数,但是在内部它需要一些可以只能被提取异步:如何在需要同步返回的函数中使用异步值
// Gets something asynchronously
const getValueAsync = Promise.resolve('value')
// Needs to return a value synchronously
const returnSyncValue =() => {
// Needs the async data here
getValueAsync
// Modify it
// Return the result sync
}
// So that this will work:
const value = returnSyncValue()
我正在使用最新版本的节点。有没有办法在不改变returnSyncValue的实现的情况下实现这一点(即使它同步)?
这是不可能的异步同步 - 你只需要考虑一秒钟,以实现为什么 –
http://stackoverflow.com/questions/14220321/how-do-i-return-the-response异步调用 – dm03514
异步性是病毒性的:一旦你使用了一个异步函数,其他的一切都必须是异步的。 – robertklep