-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js.html
128 lines (100 loc) · 3.17 KB
/
queue.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: queue.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: queue.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const partial = (fn, ...args) => (...rest) => fn(...args, ...rest);
/**
* Queue constructor
*/
const Queue = function() {
this.tasks = [];
this.onDone = result => result;
};
/**
* Add a new task to the queue
* @param {Function} fn - task to add to the queue
* @param {Array} args - array of arguments for task
* @returns {this}
*/
Queue.prototype.pushTask = function(fn, args = []) {
const task = partial(fn, ...args);
this.tasks.push(task);
return this;
};
/**
* Set function that will be done after the all task of queue
* @param {Function} fn - function to be done
* @returns {this}
*/
Queue.prototype.done = function(fn) {
this.onDone = fn;
return this;
};
/**
* Completion of tasks in the queue and save their result
* @returns {Promise}
*/
Queue.prototype.doTasks = function() {
let taskIndex = 0;
return new Promise(done => {
const taskResult = index => result => {
this.tasks[index] = result;
taskIndex++;
nextTask();
};
const nextTask = () => {
if (taskIndex < this.tasks.length) {
this.tasks[taskIndex]()
.then(taskResult(taskIndex))
.catch(taskResult(taskIndex));
} else {
done(this.onDone(this.tasks));
}
};
nextTask();
});
};
/**
* Main function to export
* @returns {Queue} new instance of Queue
* @example
* const q = queue();
const createTask = v => new Promise(res => setTimeout(() => res(v), v));
q.pushTask(createTask, [100])
.pushTask(createTask, [200])
.pushTask(createTask, [300]);
q.done(result => console.log(result));
q.doTasks();
*/
const queue = () => new Queue();
module.exports = queue;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AsyncEmitter.html">AsyncEmitter</a></li></ul><h3>Global</h3><ul><li><a href="index.html#asyncMemoize">asyncMemoize</a></li><li><a href="index.html#map">map</a></li><li><a href="index.html#Queue">Queue</a></li><li><a href="index.html#queue">queue</a></li><li><a href="index.html#reduce">reduce</a></li><li><a href="index.html#retry">retry</a></li><li><a href="index.html#series">series</a></li><li><a href="index.html#some">some</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Mon Jan 04 2021 15:21:06 GMT+0200 (GMT+02:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>