【问题标题】:Javascript Wait for Promise to be resolved in a different function before continuing async function for animationJavascript等待Promise在不同的函数中解析,然后继续异步函数进行动画
【发布时间】:2019-03-02 14:02:03
【问题描述】:

我有一个带有动画功能的 react-native 应用程序,我正在尝试创建一个动画辅助函数,该函数等待在另一个屏幕上解析一个值,然后继续执行并将一个值返回给动画函数。

动画助手功能取决于用户在另一个屏幕上购买或不购买订阅的结果,我正在向第二个屏幕传递一个承诺,以解决用户是否购买订阅。解决此承诺后,我想继续执行我的第一个动画辅助函数并返回用户是否订阅了我的第一个动画函数的值。

这是我的动画功能

Animated.timing(position, {
             toValue: {x: width, y: 0},
             duration: 300,
           }).start(async() => {
            const touchJustAdded = await animationHelper({props})
             this.setState({touchJustAdded: actionAllowed}) //This should update the state with the value returned from the animation helper before continuing animation. 
//touchJustAdded is used in another function to control the text shown in the animated view, so I need the value to be set here before continuing the animation.  I also don't want to continue the animation until the user returns from the BuySubscription screen. 
               Animated.timing(position, {
               toValue: {x: 0, y: 0},
               delay: 1200,
               duration: 300,
             })
           })

这是我的动画辅助函数

const AnimationHelper = async ({props}) => {
  //check if user has a subscription or other conditions are met, do stuff, then return true.   Otherwise, navigate to BuySubscription screen
  navigation.navigate('BuySubscription',{promiseCallback})
  //I want to pause execution here until the promiseCallback promise is resolved
  return subscriptionBought //I need to wait for the user to complete their action on the other screen and then somehow return the value to the animationHelper before returning here
}

这是我创建承诺的方式:

promiseCallback = async (subscriptionBought) => {
  return new Promise((resolve,reject) => {
    resolve( subscriptionBought)
  })
}

当用户离开该屏幕时,该承诺会在 BuySubscription 屏幕中使用此代码解析:

  this.props.navigation.state.params
    .promiseCallback(this.state.subscriptionBought)

我想在继续执行 AnimationHelper 之前等待承诺得到解决。

感谢任何帮助!我已经搜索过,但我不明白如何在 Animation 函数中等待 promiseCallback 而不调用它。

【问题讨论】:

  • 您的意思是要打开另一个屏幕,并在此屏幕中等待,直到用户进行支付或取消等操作?
  • 我想等待用户完成支付或者在另一个屏幕上取消它,然后再继续第一个动画功能。我也编辑了我的问题以使其更清楚。

标签: reactjs react-native react-native-navigation


【解决方案1】:

你可以像这样不经承诺地做到这一点:

屏幕1:

promiseCallback = (subscriptionBought) => {
  alert("Result: " + subscriptionBought)
  // continue your animation here
}

const Animation = async ({props}) => {
  //start animation
  navigation.navigate('BuySubscription', {promiseCallback: this.promiseCallback})

  return
}

现在在您的BuySubscription 屏幕呼叫promiseCallbackcomponentWillUnmount 事件:

componentWillUnmount() {
  this.props.navigation.state.params.promiseCallback(this.state.subscriptionBought);
}

【讨论】:

  • 嗨,这不太行,因为我需要返回用户是否从我的动画函数中购买了订阅的值。动画函数实际上是由另一个执行实际动画的函数调用的。抱歉命名混乱 - 我将使用此要求更新我的问题以返回值。如果有一种方法可以等待在 AnimationHelper 函数中调用的 promiseCallback 函数,那么这可以工作,但我没有看到这样做的方法。
  • @theoryweary 我想你想在 AnimationHelper 函数中等待,对吧?如果是这样,那是不可能的,也不是好办法。
  • @theoryweary 您不能等待用户操作在函数内部完成。您可以将AnimationHelper 定义为state,并在用户完成操作后将subscriptionBought 设置为该状态
【解决方案2】:

试试这个

const functionToAwait = async ({props}) => {
  let hold = await promiseCallback();
  if(hold){
  navigation.navigate('BuySubscription',hold)
  //I want to pause execution here until the promiseCallback promise is resolved
  return
  }
}

【讨论】:

  • 感谢您的回答。然而这似乎不起作用,因为 let hold = await buySubscriptionCallback() 立即解决了这个承诺。相反,我想等到用户购买订阅或取消时在另一个屏幕上解决承诺,然后再继续第一个动画功能。我还需要在解决callbackPromise时传入用户是否购买了订阅。我在我的问题中编辑了函数名称以使其更加清晰。谢谢你!!
【解决方案3】:

您还必须在您的承诺中await function。并且还必须等待您调用 promise 函数的位置。

e.g.

promiseCallback = async (subscriptionBought) => {
  return await new Promise((resolve,reject) => {
    resolve( subscriptionBought)
  })
}

Please see my answer here

【讨论】:

  • 您好,我尝试添加此逻辑,但我的 AnimationHelper 功能仍会立即运行,因此我无法返回用户是否使用此功能在 AnimationHelper 功能中购买了订阅。我只是在上面编辑了我的问题以使其更清楚。我还查看了您的其他答案,但我无法在内部创建带有 promiseCallback 的 Main 函数,因为我不想从 AnimationHelper 方法调用 promiseCallback。我需要 BuySubscription 屏幕来调用 promiseCallback。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多