Created
July 25, 2017 19:54
-
-
Save suguru03/9c219197887764467e3cf93f420b7071 to your computer and use it in GitHub Desktop.
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
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