DefinitelyTyped/types/p-queue/p-queue-tests.ts
2018-01-17 12:36:14 -08:00

50 lines
1.1 KiB
TypeScript

import PQueue = require('p-queue');
const queue = new PQueue({concurrency: 1});
queue.add(() => Promise.resolve('sindresorhus.com')).then((sindre) => {
const str: string = sindre;
});
queue.addAll([() => Promise.resolve('oh'), () => Promise.resolve('hi')]).then(r => {
r.indexOf('h');
});
Promise.resolve((): Promise<string> => Promise.resolve('unicorn'))
.then(task => queue.add(task, {priority: 5}))
.then(unicorn => {
const str: string = unicorn;
});
queue.onEmpty().then(() => {});
queue.onIdle().then(() => {});
queue.start();
queue.pause();
queue.clear();
let num: number;
num = queue.size;
num = queue.pending;
const paused = queue.isPaused;
class QueueClass implements PQueue.QueueClass<{ any: string }> {
private readonly queue: Array<() => void>;
size = 0;
constructor() {
this.queue = [];
}
enqueue(run: () => void, options: { any: string }) {
this.queue.push(run);
}
dequeue() {
return this.queue.shift();
}
}
const queue2 = new PQueue({queueClass: QueueClass});
queue2.add(() => Promise.resolve(), {any: 'hi'});