Last active
January 10, 2019 13:31
-
-
Save viniciuskneves/086632be8482e2cd842669d202414249 to your computer and use it in GitHub Desktop.
Yet another post about async/await and promises
This file contains 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
// https://dev.to/viniciuskneves/yet-another-post-about-asyncawait-and-promises-49gg | |
// Example 1 | |
function doSomething() { | |
return Promise.resolve('#2'); | |
} | |
const promise = doSomething().then(console.log); | |
console.log('#1'); | |
// Example 2 | |
(async function () { | |
async function doSomething() { | |
return '#2'; | |
} | |
const result = await doSomething(); | |
console.log(result); | |
console.log('#1'); | |
})(); | |
// Example 3 | |
function doSomething() { | |
return Promise.resolve('#2'); | |
} | |
const promise = doSomething().then(data => { | |
console.log(data); | |
console.log('#1'); | |
}); | |
// Example 4 | |
(async function () { | |
function doSomething() { | |
return Promise.resolve('#2'); | |
} | |
const result = await doSomething(); | |
console.log(result); | |
console.log('#1'); | |
})(); | |
// Example 5 | |
(async function () { | |
async function doSomething() { | |
return '#2'; | |
} | |
const result = doSomething().then(console.log); | |
console.log('#1'); | |
})(); | |
// Example 6 | |
(async function () { | |
function doSomethingPromise() { | |
return Promise.resolve('#2'); | |
} | |
async function doSomethingAsync() { | |
return '#2'; | |
} | |
const somePromise = doSomethingPromise(); | |
const someAsync = doSomethingAsync(); | |
console.log(somePromise); | |
console.log(someAsync); | |
})(); | |
// Example 7 | |
/* | |
async function myFunction(...params) { | |
const value1 = await someFunction1(); | |
const value2 = await someFunction2(...params); | |
// The following code needs to sync | |
const whatNow = async () => { | |
await value1.forEach(async v1 => { | |
someFunction3(v1); | |
someFunction4(v1); | |
value2.forEach(v2 => { | |
someFunction5(v1, v2); | |
}); | |
}); | |
} | |
whatNow(); | |
} | |
*/ | |
(async function() { | |
async function someFunction1() { | |
return ['f1_1', 'f1_2', 'f1_3']; | |
} | |
async function someFunction2() { | |
return ['f2_1', 'f2_2', 'f2_3']; | |
} | |
async function someFunction3(v1) { | |
return `f3+${v1}`; | |
} | |
async function someFunction4(v1) { | |
return `f4+${v1}`; | |
} | |
async function someFunction5(v1, v2) { | |
return `f5+${v1}+${v2}`; | |
} | |
const [value1, value2] = await Promise.all([someFunction1(), someFunction2()]); | |
const promises = flatten(value1.map(v1 => [ | |
someFunction3(v1), | |
someFunction4(v1), | |
value2.map(v2 => someFunction5(v1, v2)), | |
])); | |
const result = await Promise.all(promises); | |
console.log(result); | |
})(); | |
// https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat | |
function flatten(arr) { | |
return arr.reduce(function (flat, toFlatten) { | |
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment