Working with promises inside if / else

I have a conditional statement in which I need to perform one of two operations, and then continue after what operation was allowed. So my code is as follows:

if (shoud_do_thing_a) { //should_do_thing_a is just a variable that determines which function to call. it is not a promise
  do_thing_a()
} else {
  do_thing_b()
}

// more code

The problem is that, as do_thing_awell as do_thing_breturn the Promises, and I can not move forward as long as the implementation is not met. The best way to solve this problem:

var more_code = function () { 
  // more code
}

if (shoud_do_thing_a) {
  do_thing_a().then(more_code)
} else {
  do_thing_b().then(more_code)
}

I do not like this structure. It is hard to follow because you need to jump to find where it is more_codedefined (imagine that I have this type of control flow in several places), and not just the ability to continue reading.

Is there a better way to handle this type of thing in javascript?

+4
source share
8

:

var more_code = function () { 
    // more code
}

var do_thing;
if (shoud_do_thing_a) {
  do_thing = do_thing_a()
} else {
  do_thing = do_thing_b()
}

do_thing.then(more_code)

async/wait

async function someFunc() {
    var more_code = function () { 
        // more code
    }

    if (shoud_do_thing_a) {
        await do_thing_a()
    } else {
        await do_thing_b()
    }

    more_code()
}
+6

raw Promises async/await ( , babel/typescript ..), , :

function something() {
  return Promise.resolve()
    .then(() => {
      if (should_do_thing_a) {
        return do_thing_a();
      }
      else if (should_do_thing_b) {
        return do_thing_b();
      }
    })
    .then(some_more_code);
}

, Promises, , . - , , .

, , Promises, "" Promises.

+1

async/await

async function fn() {
  let p, result;
  if (shoud_do_thing_a) {
    p = await do_thing_a()
  } else {
    p = await do_thing_b()
  }
  if (p) {
    result = more_code();
  }
  return result
}
0

if:

var promise;

if (shoud_do_thing_a) {
  promise = do_thing_a();
} else {
  promise = do_thing_b();
}

promise.then(more_code);
0
var promise = shoud_do_thing_a? do_thing_a: do_thing_b

promise().then(function () {
// more code    
})
0

, .

(async () => {
  const should_do_thing_a = true

  const do_thing_a = function() {
    return new Promise(function(resolve, reject) {
      resolve('a')
    })
  }

  const do_thing_b = function() {
    return new Promise(function(resolve, reject) {
      resolve('b')
    })
  }

  const result = (should_do_thing_a) ? await do_thing_a() : await do_thing_b()

  console.log(result)

})()
Run codeHide result
0
source

The way I do this is to put an if-check in another function that returns a promise. A promise is permitted by resolving other function calls in an if-else statement.

Example:

function performCheck(condition) {
    var defer = $q.defer();
    if (condition) {
        doThingA().then(function(response) {
            defer.resolve(response);
        });
    } else {
        doThingB().then(function(response) {
            defer.resolve(response)
        });
    }
    return defer.promise;
}
performCheck(condition).then(function(response) {
    //Do more code.
});

In my opinion, I would prefer this method, because now this function can be used in several places where you check the state, reducing code duplication, and it is easier to follow.

You can reduce this with

function performCheck(condition) {
    var defer = $q.defer();
    var doThisThing = condition ? doThingA : doThingB;
    doThisThing().then(function (response) {
        defer.resolve(response);
    });
    return defer.promise;
}
performCheck(condition).then(function(response) {
    //Do more code.
});
0
source
more_code = miFunc() => return new Promise((resolve, reject) => ... });

Solution 1

const waitFor = should_do_thing_a ? do_thing_a() : do_thing_b();
waitFor.then(...).catch(...)

Decision 2

let waitFor = Promise.resolve();

if (do_thing_a) {
    waitFor = do_thing_a();
} else {
    waitFor = do_thing_b();
}

waitFor.then(...).catch(...);
-1
source

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


All Articles