Using Q library in browser

前端 未结 2 426
-上瘾入骨i
-上瘾入骨i 2021-02-09 15:37

I need to use Q library (http://documentup.com/kriskowal/q/) in the browser. I would like to use RequireJS to load this library, but I don\'t have any

2条回答
  •  执念已碎
    2021-02-09 16:40

    The proper AMD way of doing this would be (borrowed example code from @Eamonn O'Brien-Strain):

    requirejs.config({
      paths: {
        Q: 'lib/q'
      }
    });
    
    function square(x) {
      return x * x;
    }
    
    function plus1(x) {
      return x + 1;
    }
    
    require(["Q"], function (q) {
      q.fcall(function () {
        return 4;
      })
        .then(plus1)
        .then(square)
        .then(function (z) {
          alert("square of (value+1) = " + z);
        });
    });
    

    This way Q doesn't leak to the global scope and it's easy to find all modules depending on this library.

提交回复
热议问题