我正在编写一个React Native应用程序。有没有办法在返回值之前在非异步函数中解析Promise?
样品:
const f = () => {
return new Promise((resolve) => {
resolve('baz')
})
}
const foo = () => {
const a = f() // a is now a Promise
return a
};
console.log(foo) // `foo` is a Promise, I need 'baz'
我想找到一种方法从f
中获取已解析的值,但我无法使foo()
异步。有没有办法实现这个目标?
如果没有,任何想法如何绕过这个?
这是我的真实代码,有一些清理:
const stackNavigator = new StackNavigator(...)
const defaultGetStateForAction = stackNavigator.router.getStateForAction
stackNavigator.router.getStateForAction = (action, state) => {
let screenToRequest = undefined
let defaultState = undefined
const isInitial = action.type === 'Navigation/INIT'
if (isInitial) {
defaultState = MainTabs.router.getStateForAction('Navigation/INIT')
screenToRequest = defaultState.routes[defaultState.index].routeName
} else if (action.type === 'Navigation/NAVIGATE') {
screenToRequest = action.routeName
}
if (screenToRequest) {
const screen = screens.getScreen(screenToRequest)
/**
* If screen requires login, check if user is logged in
*/
if (screen.screen.loginRequired) {
/**
* Here I would like to read data from AsyncStorage with
* `await` but that's not possible due to this function
* not being `async` and it cannot be since router does not
* seem to support it.
*
* getSession() reads data from AsyncStorage. If I could
* `await getSession()`, I would not have a problem.
*/
const session = getSession()
// Use `session` to determine if user is logged and act accordingly
}
}
return defaultGetStateForAction(action, state);
};
答案 0 :(得分:0)
快速&脏:您可以将 foo()
转换为轮询功能。
只需创建一个 setInterval
,每隔一段时间轮询一次,并检查Promise结算后由f()
更新的值。
编辑:
对不起,我想我误解了你的问题。您不能使其成为async
。那么,你可以这样做:
const foo = async () =>{
await const a = f() // a is now a Promise
return a // a will not be returned until f() resolves
};
这将使函数同步(阻塞)。当你打电话时,如果你需要保持同步性,你也会想await
:
{
bar: async () =>{
await foo();
return; // dont return until foo() resolves
}
}
答案 1 :(得分:0)
感觉您可能想重新考虑一下您的架构。如果您正在执行操作(假设网络请求)应用程序的其他部分需要该数据才能运行,那么您只有两个选择。您可以暂停所有内容并等待(异步/等待),也可以让您的应用程序在没有该数据的情况下正常继续,然后在可用时自行更新。这两个选项都很棒,而且只取决于你想要做什么。
在React / React Native应用程序中,当您有一个等待数据从网络请求返回的UI时,会经常使用第二个选项。您在发出请求时不会冻结UI,而是使用一些消息表示数据即将到来。在解决了promise的异步函数中,您必须将该数据放在患者UI可以读取的某个位置。这就是Redux或Mobx等状态管理系统的用武之地。它们为您提供存储数据的位置,然后他们调度事件以提醒您UI已准备好,现在可以使用所有漂亮的数据重新渲染。 / p>
希望在某种程度上有所帮助。