Using the Q library in a browser

I need to use the Q library ( http://documentup.com/kriskowal/q/ ) in the browser. I would like to use RequireJS to download this library, but I do not know how to do this. I know how to load my own module, but I cannot do it with Q It has some function:

 (function (definition) { //some another code here*** // RequireJS } else if (typeof define === "function" && define.amd) { define(definition); 

How can I load Q and then use it in another module?

+6
source share
2 answers

You can simply load the Q library using a script statement in HTML

 <script src="https://cdnjs.cloudflare.com/ajax/libs/q.js/1.1.0/q.js"></script> 

Then you can access it through the Q variable as follows:

 function square(x) { return x * x; } function plus1(x) { return x + 1; } Q.fcall(function () {return 4;}) .then(plus1) .then(square) .then(function(z) { alert("square of (value+1) = " + z); }); 

See how http://jsfiddle.net/Uesyd/1/ works

+3
source

AMD's correct way to do this would be (borrowed code sample 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 Q method does not flow into the global area, and it is easy to find all modules depending on this library.

+14
source

Source: https://habr.com/ru/post/952077/


All Articles