Javascript 基于其他回调函数的函数。。。自然反应

Javascript 基于其他回调函数的函数。。。自然反应,javascript,react-native,Javascript,React Native,当用户想要发布一些东西时,他必须在没有用户名和密码的情况下签名。 问题是,当CreatePost被调用时,它将调用SingUser,并且基于SingUser获取请求,它将再次调用CreatePost,让用户在登录后发布 这是在createpost组件中 CreatePost{ 获取URL{ 方法:'POST', 标题:{ 接受:'application/json', “内容类型”:“应用程序/json”, }, 正文:JSON.stringifypost }.thenresponse=>resp

当用户想要发布一些东西时,他必须在没有用户名和密码的情况下签名。 问题是,当CreatePost被调用时,它将调用SingUser,并且基于SingUser获取请求,它将再次调用CreatePost,让用户在登录后发布

这是在createpost组件中 CreatePost{ 获取URL{ 方法:'POST', 标题:{ 接受:'application/json', “内容类型”:“应用程序/json”, }, 正文:JSON.stringifypost }.thenresponse=>response.json .thenresponseJson=>{ ifresponseJson.status=='inactive'{ //签名者 }否则{ //职位 } }.catcherror=>{//稍后 };
} 您可以这样做:

const errorCodeMap = {
  USER_INACTIVE: 10,
}

const statusMap = {
  INACTIVE: `inactive`
}

const METHOD = `POST`
const APPLICATION_JSON = `application/json`

const headerDefault = {
  Accept: APPLICATION_JSON,
  'Content-Type': APPLICATION_JSON,
}

const who = `post`

async function createPost(payload, options) {
  try {
    const {
      url = ``,
      fetchOptions = {
        method: METHOD,
        headers: headerDefault,
      },
    } = options
    const {
      post,
    } = payload

    const response = await fetch(url, {
      ...fetchOptions,
      body: JSON.stringify(post)
    })

    const {
      status,
      someUsefulData,
    } = await response.json()

    if (status === statusMap.INACTIVE) {
      return {
        data: null,
        errors: [{
          type: who,
          code: errorCodeMap.USER_INACTIVE,
          message: `User inactive`
        }]
      }
    } else {
      const data = someNormalizeFunction(someUsefulData)

      return {
        data,
        errors: [],
      }
    }
  } catch (err) {

  }
}

async function createPostRepeatOnInactive(payload, options) {
  try {
    const {
      repeat = 1,
    } = options

    let index = repeat
    while (index--) {
      const { data, errors } = createPost(payload, options)

      if (errors.length) {
        await signUser()
      } else {
        return {
          data,
          errors,
        }
      }
    }
  } catch (err) {

  }
}

解决它,我做了一些调整

异步CreatePost{ 试一试{ var response=wait fetchurl{ 方法:'POST', 标题:{ 接受:'application/json', “内容类型”:“应用程序/json”, }, 正文:JSON.stringifypost}; var responseJson=await response.json; ifresponseJson.status='inactive'&&postreap==true{ 后重复==假; 等待签名用户; 这是CreatePost; } 否则{ //张贴 } }catcherr{} }