Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

请问element ui里的return和 callback()有什么区别呢?好像可以一起用?也可以分开单独用?

请问element ui里的return和 callback()有什么区别呢?好像可以一起用?也可以分开单独用?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

肯定是不一样的呀。

  • return 这种是同步的,当然 return Promise 是异步的。return 时,如何调用呢?

    • 同步 var result = this.fun()
    • 异步 Promise

      var result
      this.fun().then(v=>result = v)
    • 异步 await

      var result = await this.fun()
  • callback() 这种本质上来讲是为了异步,当然同步也能做。那么 callback 时如何调用呢?

    • 同步&异步

      var result;
      this.fun((data)=>{ result = data })

通过上面可以看到,callback() 其实是提供了一种异步兼容方式,用来弥补 return 不支持异步,同时 promise 和 await 等方案兼容性的方法。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...