2.1.9 • Published 4 years ago

move-on v2.1.9

Weekly downloads
3,965
License
MIT
Repository
github
Last release
4 years ago
const moveOn = require('move-on');
<script src='move-on.js'></script>
<script>
  moveOn(list, config, onDone, onCatch);
</script>
> git clone https://github.com/devrafalko/move-on.git
> cd move-on
> npm install
> npm test        //run tests in node
> npm test err    //run tests in node with failed specs shown
> npm test deep   //run tests in node with errors' descriptions shown
const moveOn = require('move-on'); 
/* [Function] moveOn(list, config, onDone, onCatch)
   [Function] moveOn.all(list, config, onDone, onCatch)
   [Function] moveOn.each(list, config, onDone, onCatch)
   [Function] moveOn.first(list, config, onDone, onCatch) */

const list = [retrieveData, computeData, displayData];
const config = { timeout: 5000 };

moveOn(list, config, onDone, onCatch);

function retrieveData(resolve, reject, context){
  setTimeout(resolve, 1000); //asynchronous resolve
}

function computeData(resolve, reject, context){
  resolve(); //synchronous resolve
}

function displayData(resolve, reject, context){
  resolve();
}

function onDone(reject, context){}
function onCatch(context){}
const retrieveData = function(){};
const computeData = ()=>{};
const displayData = { display:()=>{} };
const list = [retrieveData, computeData, displayData.display];
const workers = {}, earnings = {}, tasks = {};
const config = {context: tasks}; //the default this reference
const list = [
  functionA, //this === tasks
  [workers, functionB], //this === workers
  [earnings, functionC] //this === earnings
];
moveOn(list, config, onDone, onCatch));
const workers = {
  addWorker: function(){},
  listEarnings: function(){}
};
const list = [
  workers.addWorker, //this !== workers
  workers.listEarnings //this !== workers
];
const displayData = function(){};
const workers = {
  addWorker: function(){},
  listEarnings: function(){}
};
const list = [ [workers, 'addWorker', 'listEarnings'], displayData ];

moveOn(list, config, onDone, onCatch));
function fetchData(resolve, reject, context){
  this.someAsyncAjaxHere((err, data) => {
    if(err) return reject(new Error('Could not read the data.'));
    this.data = data;
    return resolve();
  });
}
const moveOn = require('move-on');
const list = [requestData, parseData, displayData];
const config = {
  context:{
    table: document.getElementById('list') //accessible in all functions as this.table
  }
};

moveOn(list, config, onDone, onCatch);

//asynchronous
function requestData(resolve, reject, context) {
  getAjaxData((err, json) => {  
    if (err) return reject(err);
    this.json = json;
    resolve();
  });
}

//synchronous
function parseData(resolve, reject, context) {
  this.data = parseJSON(this.json);
  this.employees = getEmployeesList(this.data);
  this.earnings = getEarningsList(this.data);
  resolve();
}

function displayData(resolve, reject, context) {
  table.innerHTML = parseTableContent(this.employees, this.earnings);
  resolve();
}

function onDone(reject, context) {
  this.table.style.display = "table";
}

function onCatch(context, err) {
  throw new Error(`Could not get the data: ${err}`);
}
const moveOn = require('move-on');

function check(resolve, reject) {
  console.log(this.name); //Jessica
  console.log(this.age); //25
  //the [String] argument passed through the catch callback function
  if (!this.name || !this.age) return reject('The person was not defined.');
  return resolve();
}

const config = {
  context: {
    name: 'Jessica',
    age: 25
  }
};

moveOn([check], config, (reject, context) => {
  //the arrow function could not be bound to the context reference
  //but the context is still accessible as the parameter
  console.log(`New person added: ${context.name} (${context.age})yo.`);
}, (context, err) => {
  console.error(err); //The [String] argument passed through the reject callback function
});
const moveOn = require('move-on');

moveOn([a, b, c], null, onDone, onCatch);

function a(resolve, reject) {
  resolve();
}
function b(resolve, reject) {
  reject('oops!');
}
function c(resolve, reject) {
  //it's been never called!
  resolve();
}

function onDone(reject, context) {
  //it's been never called!
}

function onCatch(context, message) {
  console.log(message); //oops!
}
const moveOn = require('move-on');

moveOn([a, b, c], null, onDone, onCatch);

/* logs order:
  A before
  B before
  C before
  Done!
  C after
  B after
  A after
*/

function a(resolve) {
  console.log('A before');
  resolve();
  console.log('A after');
}
function b(resolve) {
  console.log('B before');
  resolve();
  console.log('B after');
}
function c(resolve) {
  console.log('C before');
  resolve();
  console.log('C after');
}

function onDone(reject, context) {
  console.log('Done!');
}

function onCatch(context, msg) {
  console.log(msg); //oops!
}
const moveOn = require('move-on');
moveOn([a, b, c], null, onDone, onCatch);
/* The order of functions execution:
   A, X, Y, Z, B, C, Done, Catch, B, C, Done, Catch */

function a(resolve, reject) {
  console.log('A');
  moveOn([x, y, z], null, () => resolve(), () => reject);
  resolve();
}
function b(resolve) {
  console.log('B');
  resolve();
}
function c(resolve, reject) {
  console.log('C');
  resolve();
  reject();
}
function x(resolve) {
  console.log('X');
  resolve();
}
function y(resolve) {
  console.log('Y');
  resolve();
}
function z(resolve) {
  console.log('Z');
  resolve();
}
function onDone() {
  console.log('Done');
}
function onCatch() {
  console.log('Catch');
}
const moveOn = require('move-on');

const list = [getArticle, getTagList, getCommentSection];

moveOn.all(list, { timeout: 5000, passContext: false }, onDone, onCatch);

function getArticle(resolve) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = () => {
    if (xhr.readyState == 4 && xhr.status == 200) return resolve(xhr.responseText);
  };
  xhr.open("GET", "article.js", true);
  xhr.send();
}

function getTagList(resolve) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = () => {
    if (xhr.readyState == 4 && xhr.status == 200) return resolve(xhr.responseText);
  };
  xhr.open("GET", "tags.js", true);
  xhr.send();
}

function getCommentSection(resolve) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = () => {
    if (xhr.readyState == 4 && xhr.status == 200) return resolve(xhr.responseText);
  };
  xhr.open("GET", "comments.js", true);
  xhr.send();
}

function onDone(reject, context, map) {
  let article = JSON.parse(map[0][0]);
  let tags = JSON.parse(map[1][0]);
  let comments = JSON.parse(map[2][0]);
}

function onCatch(err) {
  throw new Error(err);
}
const moveOn = require('move-on');
const fs = require('fs');
const path = require('path');

const list = [getContentsList, copyFiles];
const config = {
  passContext: false,
  context: {
    copyFrom: './modules',
    copyTo: './prod/modules'
  }
};

moveOn(list, config, onDone, onCatch);


function getContentsList(resolve, reject) {
  fs.readdir(this.copyFrom, (err, files) => {
    if (err) return reject(`Could not get the access to the "${this.copyFrom}" path.`);
    resolve(files); //the files object will be passed through the second function
  });
}

function copyFiles(resolve, reject, files) {
  const list = [];
  //the moveOn.each will take the same user context to get the access to the paths
  const config = { context: this, passContext: false };
  //creating the list of chained functions for each files item:
  for (let file of files) list.push((resolve, reject) => {
    let fromPath = path.resolve(this.copyFrom, file);
    let toPath = path.resolve(this.copyTo, file);
    fs.copyFile(fromPath, toPath, (err) => {
      //the reject call does not abort the module execution in moveOn.each method
      if (err) return reject(`The file "${file}" could not be copied.`);
      resolve(file); //the file path is added to the [Resolved] map, accessible in the final done callback function
    });
  });
  //the inner moveOn.each module - as the done callback - calls the resolve callback of the second copyFiles function with [Resolved] map argument
  moveOn.each(list, config, (reject, map) => resolve(map), (err) => console.warn(err));
}


function onDone(reject, map) {
  //the [Resolved] map contains the collection of all passed files paths
  //the missing property contains the indeces of all moveOn.each chained functions that have been rejected
  let message = !map.missing.length ? 'All files have been successfully moved.' : 'The files have been moved with some errors.';
  console.log(message);
}

function onCatch(err) {
  throw new Error(err);
}