helpers.js 490 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. const {Writable} = require('stream');
  3. const streamToArray = array =>
  4. new Writable({
  5. objectMode: true,
  6. write(chunk, encoding, callback) {
  7. array.push(chunk);
  8. callback(null);
  9. }
  10. });
  11. const delay = (fn, ms = 20) => async (...args) =>
  12. new Promise((resolve, reject) => {
  13. setTimeout(() => {
  14. try {
  15. resolve(fn(...args));
  16. } catch (error) {
  17. reject(error);
  18. }
  19. }, ms);
  20. });
  21. module.exports = {streamToArray, delay};