首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同时调用多个"measure“操作

同时调用多个"measure“操作
EN

Stack Overflow用户
提问于 2017-10-11 17:46:57
回答 1查看 777关注 0票数 3

我必须借助measure()函数来测量几个值。

因为它是异步操作,所以我只能写:

代码语言:javascript
复制
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或其他工具,例如:

代码语言:javascript
复制
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}
EN

回答 1

Stack Overflow用户

发布于 2017-10-12 00:02:44

我找到了一种解决方案。

measure()不是promise,但有带回调的函数:

代码语言:javascript
复制
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   
  }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46685001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档