Is there an elegant way to avoid code duplication with Promise?

Related searches

I'm looking for the best way to handle this case:

if (/* condition */) {
  /* do something */
} else {
  asyncFunc().then(/* do the same thing */);

I though of using a stored callback but this is not very readable...

Like any if/else where you'll be doing the same thing in both branches, you should put repeated code outside of the conditional. This can be done easily with async/await like this:

if (!/* condition */) {
    await asyncFunc();
/* do something either immediately or after the asyncFunc resolves */

Elegant ways to handle if(if else) else, I can't promise I'll accept it, but it deserves votes! It means such and such is something you should avoid most of the time, but not something you about 3 levels deep, duplicating code or long conditions, goto may simply end up being less evil. Of course, this makes the code slightly more complex in a different way. The only way that I can see to do this without duplicating vast chunks of code is to use the really ugly and frowned upon method of declaring the code in an include file which is then intentionally included multiple times. The following works to demonstrate the concept:

Move your common code to function

function someThing() {
/* do something */
if (/* condition */) {
} else {

code duplication in conditional Promise, I would like to be able to make changes in a single place rather than two, when I update the code. So, how can I avoid code duplication while� Another way to look at the “avoiding if/else” statement is to think in terms of decisions. The objective is to avoid confusion when following code. When you have multiple if/else statements that are nested, it becomes hard to understand the functions main objective. The programmer writing the code understands the logic now. BUT, when that

You could create a small object that handles your statements for you, which also contain the functions you need. This was you can recycle/append any functionality you need and expand whenever you need to.

    let App = {
        process: function (obj) {
            if (obj.param) {
            } else {
                App.asyncThing().then((res) => { App.funcA(res) });
        asyncThing: function () { /*..*/ },
        funcA: function () { /*...*/ },
    App.process({param: true});

Asynchronous Programming :: Eloquent JavaScript, The programs we have seen so far are things that will keep the processor busy until they Most people are aware of the fact that crows are very smart birds. The easiest way to create a promise is by calling Promise.resolve . A network request may fail, or some code that is part of the asynchronous computation may � In Angular application, we can use Rx.js to leverage power of Observables, Subject, BehaviorSubject etc for writing asynchronous code in elegant way. If you are from C# background, you might be

You can convert first case to promise

(YourCondition ? Promise.resolve(someValue) : asyncFunc()).then(v => ...)

Synchronize your asynchronous code using JavaScript's async , But first of all, since Promises are the foundation of Async functions, to be able to it changed the way developers use to write asynchronous code. We need to call .catch on the Promise and duplicate our error handling code, which and elegant than a console.log in your production-ready code (right?) JavaScript code modules let multiple privileged JavaScript scopes share code. For example, a module could be used by Firefox itself as well as by extensions, in order to avoid code duplication.

Practical Ways to Write Better JavaScript, Promises allow you to write asynchronous logic while avoiding the nesting a Promise directly since an async function is really just a fancy Promise wrapper. This also means the async/await code and the Promise code are� If this is a problem, or even if not, is there a way to avoid duplicating the named ranges during the copy or to clean them up afterwards? I thought about looping through the names collection, testing for duplicate names with the same .refersto and delete any sheet level names if there was a workbook level named range with the same name.

As seen in the above code, you made an API call using the helper method make_API_call. This call returns a promise when resolved with a list of users. Once this promise has been resolved, users returned from the promise resolution is iterated to get list of posts created by users. This creates an array of promise which is returned.

how to I specify Promise as a global so it can be consumed by the default __awaiter function? There are two ways: 1. Polyfill Promise. use a Promise polyfill so Promise is globally defined at runtime (e.g. es6-promise or similar) Ensure the TypeScript project includes ambient Promise typings, (e.g. use option "lib": ["es2015.promise"]) 2.