Created
October 7, 2016 22:58
-
-
Save klabarge/000df08e082a145e849870a3f73108df to your computer and use it in GitHub Desktop.
Queue print requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function promiseLoop() { | |
var printData = [ | |
{ "type": "pdf", "data": "assets/1.pdf" }, | |
{ "type": "pdf", "data": "assets/2.pdf" }, | |
{ "type": "pdf", "data": "assets/3.pdf" }, | |
{ "type": "pdf", "data": "assets/4.pdf" }, | |
{ "type": "pdf", "data": "assets/5.pdf" } | |
]; | |
var printers = [ | |
{ "printer": "PDFCreator" }, | |
{ "printer": "PDFCreator" }, | |
{ "printer": "PDFCreator" }, | |
{ "printer": "PDFCreator" }, | |
{ "printer": "PDFCreator" } | |
]; | |
var chain = []; | |
for(var i = 0; i < printData.length; i++) { | |
(function(i_) { | |
//setup this chain link | |
var link = function() { | |
return qz.printers.find(printers[i_].printer).then(function(found) { | |
console.log("Printing" + printData.data); | |
return qz.print(qz.configs.create(found), [printData[i_]]); | |
}); | |
}; | |
chain.push(link); | |
})(i); | |
//closure ensures this promise's concept of `i` doesn't change | |
} | |
var lastLink = null; | |
chain.reduce(function(sequence, link) { | |
lastLink = sequence.then(link); | |
return lastLink; | |
}, qz.websocket.connect()); | |
//this will be the very last link in the chain | |
lastLink.catch(function(err) { | |
console.log(err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment