Javascript 处理及;回报多个承诺 简要说明
我目前正在努力掌握以下实施的结构:Javascript 处理及;回报多个承诺 简要说明,javascript,ecmascript-6,promise,es6-promise,Javascript,Ecmascript 6,Promise,Es6 Promise,我目前正在努力掌握以下实施的结构: // Method from API Class (layer for communicating with the API) call() { // Return axios request BUT handle specific API errors e.g. '401 Unauthorized' // and prevent subsequent calls to `then` and `catch` } // Method from
// Method from API Class (layer for communicating with the API)
call() {
// Return axios request BUT handle specific API errors e.g. '401 Unauthorized'
// and prevent subsequent calls to `then` and `catch`
}
// Method from Form Class (used for all forms)
submit() {
// Call the `call` method on the API class and process
// the response.
// IF any validation errors are returned then
// process these and prevent subsequent calls to `then`
// and `catch`
}
// Method on the component itself (unique for each form)
onSubmit() {
// Call the `submit` method on the Form class
// Process the response
// Handle any errors that are not handled by the parent
// methods
}
我是这样实施的:
// Method from API Class (layer for communicating with the API)
call() {
// The purpose of this is to execute the API request and return
// the promise to the caller. However, we need to catch specific
// API errors such as '401 Unauthorized' and prevent any subsequent
// `then` and `catch` calls from the caller
return new Promise((resolve, reject) => {
this.axios.request(request)
.then(response => {
resolve(response); // Do I actually need to do this?
})
.catch(error => {
// Here we need to handle unauthorized errors and prevent any more execution...
reject(error);
});
});
}
// Method from Form Class (used for all forms)
submit() {
// The purpose of this is to call the API, and then, if it
// returns data, or validation errors, process these.
return new Promise((resolve, reject) => {
api.call()
.then(response => {
// Process form on success
this.onSuccess(response.data);
resolve(response.data);
})
.catch(error => {
// Process any validation errors AND prevent
// any further calls to `then` and `catch` from
// the caller (the form component)
this.onFail(error.response.data.error.meta);
reject(error);
})
.then(() => this.processing = false); // This MUST run
});
}
// Method on the component itself (unique for each form)
onSubmit() {
this.form.submit()
.then(response => {
// This should only run if no errors were caught in
// either of the parent calls
// Then, do some cool stuff...
});
}
问题
我的评论应该解释我试图实现的目标,但只是想说清楚:
- 如何捕获某些错误,然后防止从调用类/组件运行对
和then
的任何进一步调用catch
- 每次我返回一个新承诺时,是否确实需要创建一个新承诺
- 我知道
已经返回了一个axios.request
,但是我不知道如何访问Promise
和resolve
方法,而不使用新的reject
包装它。如果这是错误的,请随时更正Promise
:
call() {
return this.axios.request(request)
.then(response => {
// ...
})
.catch(error => {
// ...
});
}
如何捕获某些错误,然后防止从调用类/组件运行对then
和catch
的任何进一步调用
你没有。如果你要回报一个承诺,它必须解决(解决或拒绝)。两者都涉及后续处理程序的运行。承诺就是:承诺提供一个值(解决方案)或一个错误(拒绝)
您可能缺少的关键概念(很多人都有!)是then
和catch
returnnew承诺,这些承诺会根据其处理者的行为得到解决/拒绝
您可以使用catch
处理程序:
将拒绝转化为解决方案
将一个错误的拒绝转换为另一个错误的拒绝
将结果完全建立在另一个承诺的结果之上
…但您不能抑制对后续回调的调用
您可以使用然后处理程序来:
将一个值的分辨率转换为另一个值的分辨率
将分辨率转换为拒绝
将结果完全建立在另一个承诺的结果之上
例如,如果您有一个可以纠正的错误条件(这是相对罕见的,但会发生),您可以这样做
.catch(error => {
if (/*...the error can be corrected...*/) {
return valueFromCorrectingTheProblem;
}
throw error; // Couldn't correct it
})
如果返回值(或解析的承诺),则由catch
返回的承诺将与该值解析。如果抛出(或返回拒绝的承诺),则catch
返回的承诺将拒绝
每次我返回一个新的承诺时,是否真的需要创建一个新的承诺
不,见上文。(问得好。)
我知道axios.request
已经返回了一个承诺,但我不知道如何访问resolve
和reject
方法而不使用新的承诺
你没有;使用然后
和捕获
。他们返回一个新的承诺,该承诺将根据处理程序中发生的情况予以解决/拒绝。避免使用新的承诺。你可以做返回这个.axios.request(request).catch(…)
@jfriend00这就是我问这个问题的原因之一,我讨厌上面的代码,我知道它不好,但我不知道解决方案是什么。您能告诉我吗?什么是“并阻止后续调用然后和捕获”的意思?你的职能是回报承诺。根据定义,then
和catch
处理程序将运行。@jfriend00这是我以前尝试过的,但是我在从调用类调用catch
和时遇到了问题。有没有办法终止这个承诺?嗯,你的一大堆问题没有意义,这就是为什么我没有尝试回答。正如TJ所说,如果您返回的是一个承诺,则不会阻止.then()
和.catch()
处理程序运行。相反,你要训练调用代码去做什么和跳过什么,并且你要确保你设置了一个解析值或拒绝原因,这会导致调用方做正确的事情。这是一个完美的解释TJ,非常感谢!真的很感激!如果我有任何问题,我会再次尝试我的实现,然后回来,但这似乎已经涵盖了我需要的一切:-D