Asynchronous function versus return New promise

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 } } 
+5
source share
1 answer

Even without async / await , you very rarely need to use new Promise() . If you use it a lot, this is typically the smell of code.

The whole point of async / await is that it avoids many situations in which you would otherwise need to work with promises explicitly.

So, if it is supported in the environment you are aiming for (Internet Explorer does not support async / await ), or you are using a transpiler, continue to use it anywhere. What is this for.

Keep in mind that this is pointless:

 catch(e) { throw e; } 

It makes no sense to catch the error just to repair it. Therefore, if you are not actually doing anything with a caught error, do not understand it:

 async function testAsyncFunction() { //DO WORK AND THROW ERROR IF THEY OCCUR return value } 

Edit: Now that you have provided an example of your code, I can more confidently answer: if your function is based on existing promises, then by all means, it is good to use async / await , and you usually should not use new Promise() :

 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() const results = await _neo4jCreateReduce.reduce(async function (acc, el) { const result = await _neo4j.session( el, _cypherReducers.runQuery( update, i, root, start, end ) ) return [...(await acc), 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; } } 
+4
source

Source: https://habr.com/ru/post/1273038/


All Articles