等到完成功能-反应本机

时间:2020-05-02 19:39:09

标签: javascript reactjs react-native

我会清楚地说明我的问题。我有一个返回json的func。函数的工作是连接到db并带来一些数据。当我单击按钮时,我将根据func的返回值执行一些操作。 在此之前,我将打印到控制台。方法也有一些日志。

enter image description here

当我单击按钮时,处理顺序如下。

  • func已启动。.
  • func已连接。.
  • 结果是:未定义
  • 功能完成。数据为{成功:“ 00”,数据:“ blablabla”}

实际上我有数据,但是我等不及要获得结果。我该如何解决?

谢谢大家

2 个答案:

答案 0 :(得分:1)

因为Javascript的本质是异步的!您需要学习JS。

高阶函数,回调,promise,异步/等待等。

答案 1 :(得分:0)

我猜想cached_count_ds_l = {} def count_digit_sum_length (s, l): k = (s, l) if k not in cached_count_ds_l: if l < 2: if s == 0: return 1 elif l == 1 and s < 10: return 1 else: return 0 else: ans = 0 for i in range(min(10, s+1)): ans += count_digit_sum_length(s-i, l-1) cached_count_ds_l[k] = ans return cached_count_ds_l[k] def nth_of_sum (s, n): l = 0 while count_digit_sum_length(s, l) < n: l += 1 digits = [] while 0 < l: for i in range(10): if count_digit_sum_length(s-i, l-1) < n: n -= count_digit_sum_length(s-i, l-1) else: digits.append(str(i)) s -= i l -= 1 break return int("".join(digits)) print(nth_of_sum(10, 1000)) 函数以异步方式工作。试试这个:

0 numbers of length 0 sum to 10
  - need longer
0 numbers of length 1 sum to 10
  - need longer
9 numbers of length 2 sum to 10
  - need longer
63 numbers of length 3 sum to 10
  - need longer
282 numbers of length 4 sum to 10
  - need longer
996 numbers of length 5 sum to 10
  - need longer
2997 numbers of length 6 sum to 10
  - answer has length 6

Looking for 1000th number of length 6 that sums to 10
  - 996 with a leading 0 sum to 10
    - Need the 4th past 99999
  - 715 with a leading 1 sum to 10
    - Have a leading 1
Looking for 4th number of length 5 that sums to 9
  - 495 with a leading 0 sum to 9
    - Have a leading 10
Looking for 4th number of length 4 that sums to 9
  - 220 with a leading 0 sum to 9
    - Have a leading 100
Looking for 4th number of length 3 that sums to 9
  - 55 with a leading 0 sum to 9
    - Have a leading 1000
Looking for 4th number of length 2 that sums to 9
  - 1 with a leading 0 sum to 9
    - Need the 3rd past 9
  - 1 with a leading 1 sum to 9
    - Need the 2nd past 19
  - 1 with a leading 2 sum to 9
    - Need the 1st past 29
  - 1 with a leading 3 sum to 9
    - Have a leading 10003

或者您可以使用异步模式

addUser

请注意,actions.addUser(Email, Password, Name, Surname) .then((res) => console.log("Result is : " + res); 需要let result = await actions.addUser(Email, Password, Name, Surname); console.log("Result is : " + result); 功能