SerializedAsyncQueue
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 - 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(); }); }); });
Developer | Mnicpt |
Username | mnicpt |
Uploaded | July 30, 2022 |
Rating | 3 |
Size | 2,800 Kb |
Views | 36,432 |
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!
Name | Size |
Fading Divider | 1,728 Kb |
Contact Snippet | 1,815 Kb |
JavaScript Aspect | 1,648 Kb |
Simple Grid | 4,606 Kb |
A Pen by mnicpt | 3,851 Kb |
First 100 Prime Numbers | 1,615 Kb |
Angular Calculator | 3,616 Kb |
Model View Sync | 1,733 Kb |
Graph Paper Background | 2,517 Kb |
Plain JavaScript Calculator | 3,600 Kb |
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!
Name | Username | Size |
Horizontal Navigation with Floats | Gymratpacks | 5,403 Kb |
Slider | Mohammed-fawzy | 2,634 Kb |
Tab Menus | Zephyr | 3,180 Kb |
Flip test | Madhes | 1,635 Kb |
A Pen by Alex Bergin | Abergin | 3,347 Kb |
Sticky Navbar | Phantomesse | 5,106 Kb |
CSS Social Media Icon | TychoBlender | 3,871 Kb |
Adding and Removing Element | Accimeesterlin | 2,119 Kb |
Exploring css spinners | Akagr | 3,569 Kb |
WRENCH - STAFF | Lolita-adams | 1,608 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!