javascript - 基于其他回调函数的函数...react-native

标签 javascript react-native

当用户想要发布某些内容时,他必须登录(没有用户名和密码)。 问题是我试图在 CreatePost() 调用时调用 SingUser() 并根据 SingUser() 获取请求,它将再次调用 CreatePost() 以让用户在登录后发帖。

这是在 createpost 组件中

CreatePost(){
    fetch(url ,{
      method :'POST',
      headers:{
        Accept:'application/json',
        'Content-Type' :'application/json',
      },
      body: JSON.stringify(post)
   }).then((response) => response.json())
  .then((responseJson)=>{
      if(responseJson.status =='inactive'){
               //SignUser
      }else{
             //post
      }
  }).catch((error)=>{ //later
  });
}

这是其他文件中的 SingUser()

async function SignUser() {
           try{
          User.vtoken = await AsyncStorage.getItem('vtoken');
          var userTemp={
            vtoken: User.vtoken,
            ntoken : User.ntoken
            }
                fetch(url,{
                    method :'POST',
                    headers:{
                      Accep : 'application/json',
                      'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(userTemp)
                  }).then((response)=> response.json()).
                then((responseJson)=>{
                        if(responseJson.path == 2){
                            Save(responseJson, userTemp);}
                            else return;
                  }).catch((error)=>{
                     });
            
       }catch(error){}
}

async function Save(result , userTemp){
    try{
        await AsyncStorage.setItem('vtoken', result.vtoken);
            User.vtoken = result.vtoken;
            userTemp.vtoken = result.vtoken;

          fetch(url,{
            method :'POST',
            headers:{
              Accep : 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(userTemp)
          }).then((response)=>response.json()).
          then((responseJson)=>{
              return 'done';
          }).catch((error)=>{})
      }
      catch(error){}
}
export {SignUser}

我希望你明白我想做什么,如果有更好的方法的话,thnx:(

最佳答案

你可以这样做:

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) {

  }
}

关于javascript - 基于其他回调函数的函数...react-native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54972435/

相关文章:

javascript - 奇怪的行为 : IE11 + AngularJS

javascript - 如果我从链接的 javascript 文件运行一些代码,是在 dom 之前还是之后执行?

javascript - 为什么 React Native 不提供 self 辩护

javascript - 花太多时间调用 promise 的 then 函数

react-native - 如何解决expo的 "NewAppScreen"问题(React native)

javascript - react .js : arrays and "Critical assumptions about the merge functions have been violated" error

javascript - 将整数拆分为随机数之和

Android React native 应用程序在 Release模式下不断停止

javascript - 将用户添加到数组

javascript - React Native 从任意数据创建 blob