Last active
April 10, 2018 22:28
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
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
Solved problems: