GithubHelp home page GithubHelp logo

Comments (4)

switer avatar switer commented on May 26, 2024 1

异步方法加锁

const fetchData = fns.lock(function (unlock, num) {
  console.log(num)
  setTimeout(function () {
      unlock()
  }, 1000)
})
fetchData(1)
fetchData(2)
setTimeout(function () {
  fetchData(3)
}, 1500)

/**
 * Print
 */
> 1
> 3

异步方法代理

const getSetting = fns.delegator(function (done) {
    console.log('wx.getSetting')
    wx.getSetting({
        success: done
    })
})

/**
 * 以下方法会同时回调
 */
request(function (res) { }) 
request(function (res) { })
request(function (res) { })
/**
 * Print
 */
> wx.getSetting

限制并行请求数,超出排队

const request = fns.queue(function (next, config) {
  config.complete = (function (fn) {
    return function (res) {
      try {
        fn && fn(res)
      } finally {
        next();
      }
    }
  })(config.complete);
  return wx.request(config);
}, 5)

from wxpage.

switer avatar switer commented on May 26, 2024

这几个方法都是腾讯视频小程序的请求优化用到的,在wxpage上没有移除:

lock: 给调用方法加锁,异步方法阻塞性调用
queue: 异步方法分批串行调用,一般用于现在并发请求数
delegator: 异步方法并发调用时,使用某个作为代理,一般用于相同请求的复用

from wxpage.

lijsh avatar lijsh commented on May 26, 2024

懂了,哪里有这几个方法的用法和实例?我看源码觉得这几个函数如果想正常使用,需要传进来的函数参数做调整,有点强耦合了

from wxpage.

lijsh avatar lijsh commented on May 26, 2024

谢谢

from wxpage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.