SerializedAsyncQueue

Developer
Size
2,800 Kb
Views
36,432

How do I make an serializedasyncqueue?

Queue that processes asynchronous functions in sequence. Functions are chained together through a callback function which is used to call the next function in the sequence. If you don't prefer to append a callback function at the end of your function parameters,. What is a serializedasyncqueue? How do you make a serializedasyncqueue? This script and codes were developed by Mnicpt on 30 July 2022, Saturday.

SerializedAsyncQueue Previews

SerializedAsyncQueue - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>SerializedAsyncQueue</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.css'> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <div class="console-aq"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

SerializedAsyncQueue - Script Codes JS Codes

/** * Queue that processes asynchronous functions in sequence. Functions are chained together * through a callback function which is used to call the next function in the sequence. * If you don't prefer to append a callback function at the end of your function parameters, * then wrap your functionality in a function with a callback parameter and provide your * implementation within. * * Properties: * queue - array of batched functions * processing - are there current functions being executed * * Methods: * enqueue - add batch of functions to queue * dequeue - remove next batch of functions to execute * processBatch - processes a single batch of functions * process - execute batch of functions in series * * */
var AsyncQueue = function() { this.queue = []; this.processing = false; this.enqueue = function(funcs) { var functions = []; for (var i = 0; i < funcs.length; i++) { functions.unshift(funcs[i]); } this.queue.unshift(functions); }; this.dequeue = function() { return this.queue.pop(); }; this.process = function() { if (this.processing) { this.enqueue(arguments); } else { this.processing = true; var functions = []; for (var i = 0; i < arguments.length; i++) { functions.unshift(arguments[i]); } var self = this; (function nextBatch() { processBatch(functions, function() { if (self.queue.length > 0) { functions = self.dequeue(); nextBatch(); } else { self.processing = false; } }); })(); } }; var processBatch = function(funcs, done) { var self = this; (function next() { if (funcs.length > 0) { var currentFunc = funcs.pop(); currentFunc.apply(self, Array.prototype.slice.call(arguments, 0).concat([next])); if (funcs.length == 0) { this.processing = false; done(); } } })(); }; } // tests describe("Async Function Queue Processor", function() { var queue = new AsyncQueue(); beforeEach(function(done) { done(); }); it("should process a single batch of asynchronous functions in series", function(done) { var value = 0; var A = function(callback) { setTimeout(function() { value = 10; callback(20); }, 200); }; var B = function(add, callback) { expect(value).toEqual(10); setTimeout(function() { var addedValue = value + add; value = addedValue; expect(value).toEqual(30); callback(); }, 200); }; var C = function(callback) { expect(value).toEqual(30); value += 10; setTimeout(function() { expect(value).toEqual(40); value += 10; callback(); }, 200); }; queue.process(A, B, C, function() { expect(value).toEqual(50); done(); }); }); it("should process multiple batches of asynchronous functions in series", function(done) { var num = 0; queue.process(function(callback) { expect(num++).toEqual(0); setTimeout(function() { expect(num++).toEqual(1); callback(); }, 100); }, function(callback) { expect(num++).toEqual(2); setTimeout(function() { expect(num++).toEqual(3); callback(); }, 100); }, function() { expect(num++).toEqual(4); }); queue.process(function(callback) { expect(num++).toEqual(5); setTimeout(function() { expect(num++).toEqual(6); callback(); }, 100); }, function(callback) { expect(num++).toEqual(7); setTimeout(function() { expect(num++).toEqual(8); callback(); }, 100); }, function() { expect(num++).toEqual(9); done(); }); }); });
SerializedAsyncQueue - Script Codes
SerializedAsyncQueue - Script Codes
Home Page Home
Developer Mnicpt
Username mnicpt
Uploaded July 30, 2022
Rating 3
Size 2,800 Kb
Views 36,432
Do you need developer help for SerializedAsyncQueue?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Mnicpt (mnicpt) Script Codes
Create amazing video scripts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!