Skip to content

Instantly share code, notes, and snippets.

@xemasiv
Last active April 10, 2018 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xemasiv/90b6d8254545c422ae6c89f207312c42 to your computer and use it in GitHub Desktop.
Save xemasiv/90b6d8254545c422ae6c89f207312c42 to your computer and use it in GitHub Desktop.
AssocPromise - Associative Promise, Promise All that returns Object with custom Object[key] support. Respects resolve(result) and reject(error) predictability.
class AssocPromise{
constructor () {
this._iterable = [];
}
push (iterate) {
if (Boolean(iterate._label) === false) {
return;
}
if (Boolean(iterate._fn) === false) {
return;
}
this._iterable.push(iterate);
return this;
}
all () {
let iterable = this._iterable;
let resultObject = {};
return Promise.all(
iterable.map((iterate) => {
return new Promise((resolve, reject) => {
let label = iterate._label;
let thisArg = iterate._thisArg;
let fn = iterate._fn;
let args = iterate._args;
fn.apply(thisArg, args)
.then((result) => {
resultObject[label] = result;
resolve();
})
.catch((error) => {
resultObject[label] = error;
resolve(true);
});
});
})
)
.then((results) => {
results.map((result) => {
if (Boolean(result) === true) {
return Promise.reject(resultObject);
}
});
return Promise.resolve(resultObject);
})
.catch(() => {
// Should never be called.
return Promise.reject(resultObject);
})
}
}
class AssocIterate{
constructor (label) {
this._label = label;
this._thisArg = null;
this._args = [];
}
thisArg (thisArg) {
this._thisArg = thisArg;
return this;
}
args (...args) {
this._args = args;
return this;
}
fn (fn) {
this._fn = fn;
return this;
}
}
// sample function
let add = (a, b) => {
return new Promise((resolve, reject) => {
resolve(parseInt(a) + parseInt(b));
})
};
let sub = (a, b) => {
return new Promise((resolve, reject) => {
resolve(parseInt(a) - parseInt(b));
// change to reject if you want to test results on reject
})
};
new AssocPromise()
.push(
new AssocIterate('test').args(1,2).fn(add)
// constructor() sets the label
// 'test' will be used as object key for this promise result result
// args() sets the arguments, optional
// thisArgs() can also be set
// fn() is the function that uses the args and thisArgs
// fn() must return a promise
// fn() must resolve or reject a result
// IDEALLY resolve(RESULT) or reject(ERROR);
)
.push(
new AssocIterate('mustbe6').args(3,3).fn(add)
)
.push(
new AssocIterate('mustbe1').args(4,3).fn(sub)
)
.all()
.then((result) => {
console.log(result);
})
.catch(console.error);
// console.log result:
// { test: 3, mustbe6: 6 }
@xemasiv
Copy link
Author

xemasiv commented Apr 10, 2018

Solved problems:

  • Async work on promise-based tasks of underlying types and forms.

@xemasiv
Copy link
Author

xemasiv commented Apr 10, 2018

Used @ https://www.npmjs.com/package/google-cloud-datastore-toolkit

Before: Request takes 1300ms to 1700ms
After: Request now takes 600ms to 700ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment