小编Ter*_*rry的帖子

道具验证中缺少功能组件中的道具

尽管已经声明了 propTypes,Eslint 仍然抛出 eslint(react/prop-types) 错误。我正在使用eslint-plugin-react

我已经查看了其他几个类似的问题以及proptypelint 规则,但它们没有解决我的问题。

import React from 'react';
import { View, Text, TouchableHighlight, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';

const PASTEL_PINK = '#dea5a4';
const PASTEL_BLUE = '#779ecb';

const Buttons = ({ onPressStart, onPressPause, onPressReset, onGoing }) => (
  <View >
    <TouchableHighlight
      onPress={onPressStart}
      disabled={onGoing}
    >
      <Text >{START_TIMER}</Text>
    </TouchableHighlight>
    <TouchableHighlight
      onPress={onPressPause}
      disabled={!onGoing}
    >
      <Text >{PAUSE_TIMER}</Text>
    </TouchableHighlight>
    <TouchableHighlight onPress={onPressReset}>
      <Text >{RESET_TIMER}</Text>
    </TouchableHighlight>
  </View>
);

Buttons.protoTypes = {
  onPressStart: PropTypes.func.isRequired,
  onPressPause: PropTypes.func.isRequired,
  onPressReset: PropTypes.func.isRequired, …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs eslint react-native react-proptypes

2
推荐指数
1
解决办法
8502
查看次数

如何实现 Promise 重试和撤消

我很好奇 API 重试和超时应该如何实现。有时,仅仅等待 api 调用然后捕获出现的任何错误是不够的。如果我需要发出一系列异步请求,如下所示:

await client
  .callA()
  .then(async () => await callB())
  .then(async () => await callC())
  .catch(err => console.error(err));
Run Code Online (Sandbox Code Playgroud)

如果其中一个承诺在中链失败,我想在几秒钟后再次尝试请求,直到尝试用完。

这是我尝试制作重试包装器。

async function retry (fn, undo, attempts, wait = 5000) {
  await fn().catch(async (err) => {

    console.error(err.message + `\n retrying in ${wait/1000} seconds...`);

    if (attempts !== 0) {
      // async timeout
      await new Promise((resolve) => {
        setTimeout(() => resolve(retry(fn, undo, attempts - 1)), wait);
      })
    } else {
      await undo()
    }
  })
}

await retry(calls, undoCalls, 10)
Run Code Online (Sandbox Code Playgroud)

callA …

javascript asynchronous promise async-await typescript

1
推荐指数
1
解决办法
1919
查看次数