UPDATE
I have read more than a dozen articles on this topic, and none of them address this fundamental issue. I am going to start listing the resource section at the end of this post.
ORIGINAL MAIL
My understanding of async is returning a promise.
MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
Inside my program, I could write something like:
function testPromise() { return new Promise((resolve, reject) => { // DO WORK reject() // IF WORK FAILS resolve() // IF WORK IS SUCCESSFUL }) } async function mainFunction() { let variable try { variable = await testPromise() } catch(e) { throw e } return variable }
I could also write testPromise as an async function, and await in the same context.
async function testAsyncFunction() { //DO WORK AND THROW ERROR IF THEY OCCUR } async function mainFunction() { let variable try { variable = await testAsyncFunction() } catch(e) { throw e } return variable }
What is considered best practice? If I want to create an asynchronous operation, should the function use return New Promise and be expected in the async function or is it expecting the async function from the async function with the same difference?
Resources
JavaScript ES 2017: Learn the Async / Await example from the example https://codeburst.io/javascript-es-2017-learn-async-await-by-example-48acc58bad65
Javascript - ES8 Introducing async/await functions https://medium.com/@reasoncode/javascript-es8-introducing-async-await-functions-7a471ec7de8a
6 Reasons Why JavaScripts Async / Await Blows Promises Away (Tutorial) https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9
---------------------- CURRENT ----------------------
export default function time_neo4jUpdate({ store, action, change, args, }) { return new Promise(async (resolve, reject) => { try { const { thing: { type }, nonValidatedArgs: { leapYear, root, start, end }, _neo4j, _cypherReducers, _neo4jCreateReduce, _timetreeSteps: { update } } = store.getState() let results = [] for (let i = 0; i < _neo4jCreateReduce.length; i++) { const result = await _neo4j.session( _neo4jCreateReduce[i], _cypherReducers.runQuery(update, i, root, start, end)) results = [...results, result] } resolve({ store, action: 'NEO4J_UPDATE', change: results, args }) } catch (e) { const m = `NEO4J TIMETREE UPDATE: Unable to complete the UPDATE step for the timetree. WHAT: ${e}` reject(m) } }) }
---------------------- ASYNC FUNCTION ----------------------
export default async function time_neo4jUpdate({ store, action, change, args, }) { try { const { thing: { type }, nonValidatedArgs: { leapYear, root, start, end }, _neo4j, _cypherReducers, _neo4jCreateReduce, _timetreeSteps: { update } } = store.getState() let results = [] for (let i = 0; i < _neo4jCreateReduce.length; i++) { const result = await _neo4j.session( _neo4jCreateReduce[i], _cypherReducers.runQuery(update, i, root, start, end)) results = [...results, result] } return { store, action: 'NEO4J_UPDATE', change: results, args } } catch (e) { const m = `NEO4J TIMETREE UPDATE: Unable to complete the UPDATE step for the timetree. WHAT: ${e}` throw m } }