Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.0 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5cdafbe72913098997531682 在 catch 中处理 Promise 失败的情况 1 301204 handle-a-rejected-promise-with-catch

--description--

当 promise 失败时会调用 catch 方法。当 promise 的 reject 方法执行时会直接调用。用法如下:

myPromise.catch(error => {
  // do something with the error.
});

error 是传入 reject 方法的参数。

注意: thencatch 方法可以在 promise 后面链式调用。

--instructions--

给 promise 添加 catch 方法。用 error 做为回调函数的参数并把 error 打印到控制台。

--hints--

应该在 promise 上调用 catch 方法。

assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.catch\(/g));

catch 方法应该有一个回调函数,函数参数为error

assert(errorIsParameter);

应该打印error到控制台。

assert(
  errorIsParameter &&
    codeWithoutSpaces.match(/\.catch\(.*?error.*?console.log\(error\).*?\)/)
);

--seed--

--after-user-code--

const errorIsParameter = /\.catch\((function\(error\){|error|\(error\)=>)/.test(__helpers.removeWhiteSpace(code));

--seed-contents--

const makeServerRequest = new Promise((resolve, reject) => {
  // responseFromServer is set to false to represent an unsuccessful response from a server
  let responseFromServer = false;
    
  if(responseFromServer) {
    resolve("We got the data");
  } else {  
    reject("Data not received");
  }
});

makeServerRequest.then(result => {
  console.log(result);
});

--solutions--

const makeServerRequest = new Promise((resolve, reject) => {
  // responseFromServer is set to false to represent an unsuccessful response from a server
  let responseFromServer = false;
    
  if(responseFromServer) {
    resolve("We got the data");
  } else {  
    reject("Data not received");
  }
});

makeServerRequest.then(result => {
  console.log(result);
});

makeServerRequest.catch(error => {
  console.log(error);
});