Skip to content

Instantly share code, notes, and snippets.

@suguru03
Created July 25, 2017 19:54
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 suguru03/9c219197887764467e3cf93f420b7071 to your computer and use it in GitHub Desktop.
Save suguru03/9c219197887764467e3cf93f420b7071 to your computer and use it in GitHub Desktop.
const _ = require('lodash');
const Aigle = require('aigle');
Aigle.mixin(_);
execute();
/*
* If `Aigle` has the same function names as `Lodash` ones, they will be ignored.
* All of them have the same functionality and are optimized for asynchronous.
* If you wanna use actual `Lodash` functions, you can call `Aigle.mixin(_, { override: true })`,
* but they must be slower then `Aigle` ones.
*/
function delay(ms, result) {
return new Promise(resolve => setTimeout(resolve, ms, result));
}
async function execute() {
await example1();
await example2();
await example3();
}
async function example1() {
const array = [1, 2, 3];
const syncResult = _.map(array, n => n * 2);
const asyncResult = await Aigle.map(array, n => delay(10, n * 2));
console.log(syncResult);
console.log(asyncResult);
}
async function example2() {
const array = [1, 2, 3];
const syncResult = _.find(array, n => n === 2);
const asyncResult = await Aigle.find(array, n => delay(10, n === 2));
console.log(syncResult);
console.log(asyncResult);
}
async function example3() {
const array = [1.1, 1.4, 2.2];
const syncResult = _.chain(array)
.map(n => n * 2) // [2.2, 2.8, 4.4]
.uniqBy(Math.floor) // [2.2, 4.4]
.sum() // [6.6]
.times() // [0, 1, 2, 3, 4, 5]
.value();
const asyncResult = await Aigle.resolve(array)
.map(n => delay(10, n * 2)) // [2.2, 2.8, 4.4]
.uniqBy(Math.floor) // [2.2, 4.4]
.sum() // [6.6]
.times(); // [0, 1, 2, 3, 4, 5]
console.log(syncResult);
console.log(asyncResult);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment