【问题标题】:Promise Chaining with Http Response带有 Http 响应的 Promise 链接
【发布时间】:2021-06-11 00:47:35
【问题描述】:

我正在尝试 Promise 链接,但我不太明白。我有这个练习我正在做的地方我必须做以下场景:

“你是在恶作剧办公室成员,在他的咖啡里加盐,你要通过/locate api查看他的位置,'locate'可以是以下任意一种:

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

你只在他在厨房时加盐,所以如果 response.body 返回“在厨房里”,你就用/addSalt 的另一个 post api 给他的咖啡加盐。

现在这个练习中有一个名为 /run 的最终 api 端点,在满足任一条件时调用它:

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

我在没有合并任何 api 或 json 的情况下尝试了以下操作(但我想这样做),它看起来像这样:

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) {

});

我不知道conditionalChaining 正在做什么,也不知道myPromiseFunction。这显然没有用,但它可以帮助概述我想要得到的东西。有什么建议吗?

我引用了这个:How to handle the if-else in promise then?

【问题讨论】:

  • 它不起作用,因为没有response 并且value 没有body 属性。

标签: javascript api promise


【解决方案1】:

myPromiseFunction() 应该解析一个包含“in the office”或“in the kitchen”的对象,该对象将 conditionalChaining 的值传递给处理,并确定下一步操作。请参阅下面的示例,我已针对您的用例进行了调整。

    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/

【讨论】:

  • 您可以使用“复制 sn-p 来回答”按钮在 Stack Overflow 上提供一个可运行的示例,而不是让人们转到另一个站点...
  • 谢谢,这是对我尝试使用的参考帖子的一个很好的解释。当我使用 json 响应做虚拟 api 端点时,现在应该简单得多。当根据返回值将 api 调用链接在一起时,您会推荐这种方法吗?
  • 我认为只要它对你有用,并且你能够理解它就可以了。将来,当您掌握了 Promise 的窍门时,您可以查看其他改进或方法,例如使用 async 和 await。当您第一次开始使用 Promise 时,执行流程可能会非常混乱,因此请先花点时间了解它。
猜你喜欢
  • 2021-07-26
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多