0

I'm trying out promise chaining but I'm not quite getting it. I have this exercise I'm doing where I have to do the following scenario:

"You are pranking an office member by adding salt to his coffee, you are to check his location with /locate api, 'locate' can be either of the following:

response.body: 'in the office'
response.body: 'in the kitchen'

You only add salt when he is in the kitchen, so if the response.body returns 'in the kitchen', you salt his coffee with another post api of /addSalt.

Now there is the final api endpoint in this exercise called /run where it is invoked when either condition is met:

/locate returns 'in the office'
/addSalt is called.

I tried the following without incorporating any api or json (I would like to though) and it looked something like this:

function myPromiseFunction() {
  //Change the resolved value to take a different path
  return Promise.resolve(true);
}

function conditionalChaining(value) {
  if (value.body === 'in the kitchen') {
    //do addSalt then run
    return addSalt(salt).then(run);
  } else {
    //run
    return run();
  }
}

function locate() {
  // fetch http get location, response body is either 'in his office' or 'in the kitchen'
  return Promise.resolve(response.body);
}

function addSalt(salt) {
  console.log("addSalt");
  return Promise.resolve("We added salt");
}

function run() {
  console.log("We are running");
  return Promise.resolve("Running");
}

myPromiseFunction().then(conditionalChaining).then(function() {
  console.log("All done!");
}).
catch(function(e) {

});

I did not get what conditionalChaining is doing nor myPromiseFunction. This obviously didn't work but it could help outline what I'm trying to get. Any tips?

I was referencing this: How to handle the if-else in promise then?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Mustafa
  • 177
  • 6
  • 20

1 Answers1

1

myPromiseFunction() should resolve an object that contains "in the office" or "in the kitchen", which passes the value for conditionalChaining to process, and determine the next action. See the example below which I have tuned for your use case.

    function locate() {
        //const msg = "in the office"; // choose either one to return, can comment the other out.
      const msg = "in the kitchen";
      var obj = {body: msg};
      return Promise.resolve(obj);
    }
    
    function addSalt(salt) {
        console.log("addSalt");
        return Promise.resolve("We added salt");
    }
    
    function run() {
        console.log("We are running");
        return Promise.resolve("Running");
    }
    
    function conditionalChaining(value) {
        if (value.body === 'in the kitchen') {
            //do addSalt then run
            return addSalt("some salt value since its undefined by u").then(run);
        } else if ( value.body === 'in the office' ) {
          console.log("hey you are in the office");
        } else {
            //run
            return run();
        }
    }
    
    // in this function, you are suppose to resolve the string "in the kitchen" or "in the office"
    // this is so that the conditionalChaining function takes in either of the string and process the next action.
    function myPromiseFunction() {
        return Promise.resolve(locate());
    }
    
    myPromiseFunction().then(conditionalChaining).then(function () {
        console.log("All done!");
    });

jsfiddle: https://jsfiddle.net/y7xrw6d0/

Niraeth
  • 315
  • 2
  • 4
  • You can use the "Copy snippet to answer" button to provide a runnable example right here on Stack Overflow instead of making people go to another site... – Heretic Monkey Mar 13 '21 at 02:20
  • Thank you, this was a good explanation on the referenced post I was trying to use. Should be much simpler now when I do dummy api endpoints with json response. Would you recommend this approach when chaining api calls together based on their return values? – Mustafa Mar 13 '21 at 02:30
  • I think its fine as long as it works for you, and you are able to understand it. In future when you got a hang of promises, you can look at other improvements or means such as using async and await. When you first start out with promises, the execution flow can be quite confusing, so just take your time to understand it first. – Niraeth Mar 13 '21 at 02:39