Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Last active June 30, 2019 12:34
Show Gist options
  • Save ramsunvtech/85fe06251d405ea6b66aa27c778cb456 to your computer and use it in GitHub Desktop.
Save ramsunvtech/85fe06251d405ea6b66aa27c778cb456 to your computer and use it in GitHub Desktop.
`isMultipleOf` Promise Function which resolves if input is multiple of number or rejects it
function isMultipleOf(input, number) {
if (input % number === 0) {
return Promise.resolve(input);
}
return Promise.reject(new Error(`not a multiple ${input}`));
}
isMultipleOf(20, 10).then(console.log, console.error)
// Factory Style
// returns a multpleOf funtion for the number provided
function multipleOfFactory(number) {
return function (input) {
return isMultipleOf(input, number);
}
}
var isMultipleOfFive = multipleOfFactory(5);
isMultipleOfFive(6).then(console.log, console.error);
// number at index is multiple of index+1
var myArray = [1, 18, 14 , 65, 43, 60]; // check if the number at index is multiple of index+1 e.g result [true true, false, false, false, true ]
// Result [true true, false, false, false, true ]
const myArrayPromise = myArray.map((value, index) => {
const i = index + 1;
return isMultipleOf(value, i).then(() => true, () => false).catch((x) => );;
});
Promise.all(myArrayPromise).then((result) => {
console.log(result)
});
Promise.all(myArrayPromise).then((result) => {
console.log(result)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment