仅在需要时返回自定义承诺 - 我做错了什么?

时间:2017-10-16 20:01:11

标签: angular

我正在尝试编写一个仅在需要时返回自定义承诺的函数。代码如下所示:

$name = $sp->find('h3', 1)->find('a', 1)->innertext;

我知道我可以将整个函数包装在login(sp: ServerProfile): Promise<any> { return this.logout(sp) // first logout .then(_ => { if (this.isAuthEnabled(sp)) { return this._login(sp) // if yes, re-login .then(succ => { this.utils.debug(`Logged in with: ${succ}`); this._isLoggedIn = true; }) } else { this._isLoggedIn = true; return new Promise ( (resolve, reject ) => {resolve(true)}) //-> problem } }) } 中,但我不想在返回承诺时不必要地包裹new Promise (resolve, reject)

如果我在this._login部分添加new Promise,我会收到此错误:

  

[ts]类型的参数&#39;(_:any)=&gt;承诺|无极&LT; {}&GT;&#39;不是   可分配给&#39;(value:any)=&gt;类型的参数无效|   PromiseLike&#39;

1 个答案:

答案 0 :(得分:0)

啊,解决方案似乎是在手工制作的承诺中指定componentDidMount() { this.props.data.subscribeToMore({ document: OrderSubscription, variables: { range: [0, 25] }, updateQuery: (prev, { subscriptionData, }) => { // If no subscription data is passed, just return the previous // result from the initial `orders` query if (!subscriptionData.data) return prev // get the data for the updated order from the subscription const updatedOrder = subscriptionData.data.orderChanged // find the index of the updated order from within the existing // array of orders from the `orders` query const existingOrderIndex = prev.orders.findIndex(order => (order.id === updatedOrder.id)) // guard for missing data if (existingOrderIndex) { // replace the old order with the updated order prev[existingOrderIndex] = updatedOrder // return orders with new, updated data return prev } return prev }, }) }

<any>