2017-10-11 5 views
0

measure()の機能を使用していくつかの値を測定する必要があります。複数の「メジャー」オペレーションを同期して呼び出す

this.refContainerView.measure((x, y, width, height, pageX, pageY) => { 
    const containerViewHeight = height 

    this.refCommentList.measure((x, y, width, height, pageX, pageY) => { 
    const commentListOffset = pageY 
    const commentListHeight = height 

    // do something 

    }) 
}) 

をし、より多くの成分を測定するために必要であれば、それはコールバック地獄のようになります。
それは非同期操作ですので、私は書くことができます。
コードを同期して書くことは可能ですか? awaitまたは他の助けを借り、例えば:

const contaierView = this.refContainerView.measure() 
const commentList = this.refCommentList.measure() 

// and then do something with 
contaierView {x, y, width, height, pageX, pageY} 
commentList {x, y, width, height, pageX, pageY} 

答えて

0

と私は解決策のようなものを見つけました。
measure()はお約束ではありませんが、コールバック機能を持っています。

measureComponent = component => { 
    return new Promise((resolve, reject) => { 
    component.measure((x, y, width, height, pageX, pageY) => { 
     resolve({ x, y, width, height, pageX, pageY }) 
    }) 
    }) 
} 

onDoSomething = async() => { 
    const [containerView, commentList] = await Promise.all([ 
    this.measureComponent(this.refContainerView), 
    this.measureComponent(this.refCommentList), 
    ]) 

    // do here with containerView and commentList measures 
    } 
} 
関連する問題