我们可以使用等待和相同的功能来实现承诺吗?

时间:2018-08-29 19:43:43

标签: javascript arrays typescript if-statement

因此,我基于建立import MyMapComponent from './Maps'; export default class HomeComponent extends React.Component { render() { const {showAlert, address, lat, lng} = this.props; return ( <MyMapComponent lat={lat} lng={lng} onBoundsChanged={this.props.handleOnBoundsChanged} /> <Button onClick={()=>this.props.getPosition()}>Usar mi ubicación actual</Button> ) } } 数组的请求而来,因此我可以调用被推入array的url和request.body,在下面的代码中尝试同时使用this.request诺言,但是当我为其他诺言调用同一函数RequestResponseHandler.processRequest时,它总是会进入spec(重复调用),如何确定是否为processRequest调用了processRequest,如果条件并进入到spec

请求

PTM

handler.ts

{ header: { lineOfBusiness: ["spec","PTM"] } , body: data }

controllet.ts

    export class RequestResponseHandler {
        public static processRequest(data: any, url: string): Promise < any > {
            const reqObject: IRequestURL = {}
            as IRequestURL;
            const lob = data.header.lineOfBusiness;
            if (lob[0] === "spec") {
                const specUrl = urlConfig + url;
                reqObject.url = specUrl;
                reqObject.body = data;
            }
            if (lob[1] === "PTM") {
                const ptmUrl = urlConfig + url;
                reqObject.url = ptmUrl;
                reqObject.body = data;
            }
         return Promise.resolve(reqObject);
        }
    }

1 个答案:

答案 0 :(得分:0)

我建议的一件事是不要做不必要的诺言功能。保持简单。

<?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="80dp" android:height="80dp" android:viewportWidth="24" android:viewportHeight="24"> <path android:pathData="M16.01 7L16 3h-2v4h-4V3H8v4h-0.01C7 6.99 6 7.99 6 8.99v5.49L9.5 18v3h5v-3l3.5-3.51v-5.5c0-1-1-2-1.99-1.99z" /> <path android:pathData="M0 0h24v24H0z" /> </vector> 不一定是一个诺言

RequestResponseHandler.processRequest

在控制器文件中

export class RequestResponseHandler {
  public static processRequest(data: any, url: string): IRequestURL {
    const reqObject: IRequestURL = {} as IRequestURL;
    const lob = data.header.lineOfBusiness;

    // I guess we can improve code below, no diff between "spec" and "ptm"
    if (lob[0] === "spec") {
      const specUrl = urlConfig + url;
      reqObject.url = specUrl;
      reqObject.body = data;
    }
    if (lob[1] === "PTM") {
      const ptmUrl = urlConfig + url;
      reqObject.url = ptmUrl;
      reqObject.body = data;
    }
    return reqObject; // return value not a promise
  }
}