首页 文章

承诺链接错误处理

提问于
浏览
1

我正在学习如何在没有库的情况下使用Promise . 根据我的阅读,我可以将Promise链接在一起,然后最后添加 .catch 以进行错误处理 .

What do I expect

因此,如果我将URL更改为某个错误的网址,我不应该捕获错误并停止整个程序继续吗?

What Am I seeing now?

当我输入一个错误的网址时,该程序只会抛出一个错误,而不是像拒绝一样处理它 .

const request = require("request");

new Promise((resolve, reject) => {
  request(
    "http://maps.googleapis.com/maps/api/geocode/json?address=321%20i%20st%20davis",
    (err, res, body) => {
      if (err) {
        reject("bad call on geo code!");
      }
      resolve(JSON.parse(body).results[0].geometry.location);
    }
  );
})
  .then(res => {
    const {lat, lng} = res;
    return new Promise((resolve, reject) => {
      request(
        `https://api.darksky.net/forecast/6fb416a8313aabd902a22558e07cc032/${lat},${lng}`,
        (err, res, body) => {
          if (err) {
            reject("bad call on darksky");
          }
          resolve(JSON.parse(body));
        }
      );
    });
  })
  .then(res => {
    const currentTemp = res.currently.temperature;
    const feelTemp = res.currently.apparentTemperature;
    const temps = {currentTemp, feelTemp};
    return new Promise((resolve, reject) => {
      request(
        "http://ron-swanson-quotes.herokuapp.com/v2/quotes",
        (err, res, body) => {
          if (err) {
            reject("bad call on quotes");
          }
          resolve({temps, body});
        }
      );
    });
  })
  .then(res => {
    console.log(
      `Today's weather is ${res.temps.currentTemp}, and it feels like ${res
        .temps
        .feelTemp}! \nAnd here is your stupid quote of the day: \n${JSON.parse(
        res.body
      )[0]}`
    );
  })
  .catch(err => {
    console.log(err);
  });

错误信息:

这不是真正有意义的,基本上错误并没有停止程序,这只是传递给下一个承诺 . 该承诺收到错误但无法解析它,因为它不是预期的JSON格式 .

SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Promise.then.then.then.res (/Users/leoqiu/reacto/playground/6_promiseMethod.js:48:74)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)

2 回答

  • 0

    当你在 if 语句中调用 reject() 时,你不要使用 else ,这样你的 resolve(JSON.parse(body).results[0].geometry.location); 仍会被执行并引发异常 .

    你可以改成这个:

    new Promise((resolve, reject) => {
      request(
        "http://maps.googleapis.com/maps/api/geocode/json?address=321%20i%20st%20davis",
        (err, res, body) => {
          if (err) {
            reject("bad call on geo code!");
            return;
          }
          resolve(JSON.parse(body).results[0].geometry.location);
        }
      );
    })
    

    这是一个常见的错误,人们认为 reject() 就像 break 或其他控制流语句一样,因为 reject() 是一种承诺控制流程 . 但是,它不会在您的块中停止执行,因此您需要 return 之后或使用 else .

    或者,我更喜欢使用 if/else 因为我认为它使逻辑更加明显:

    new Promise((resolve, reject) => {
      request(
        "http://maps.googleapis.com/maps/api/geocode/json?address=321%20i%20st%20davis",
        (err, res, body) => {
          if (err) {
            reject("bad call on geo code!");
          } else {
            resolve(JSON.parse(body).results[0].geometry.location);
          }
        }
      );
    })
    
  • 0

    基于Patrick Evans的建议......

    reject 不会阻止程序运行,因此错误消息会传递给下一个Promise,这就是为什么抛出 json 解析错误 .

    解决方案只是在拒绝中放入 return .

    if (err) {
        reject("bad call on geo code!");
        return err;
      }
    

相关问题