gpt4 book ai didi

javascript - 处理和返回多个 promise

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:58 24 4
gpt4 key购买 nike

简要说明

我目前正在努力掌握以下实现的结构:

// 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...
});
}

问题

我的评论应该解释我正在努力实现的目标,但只是为了清楚:

  • 我如何捕获某些错误,然后防止从调用类/组件运行对 thencatch 的任何进一步调用?
  • 每次返回时是否真的需要创建一个新的 Promise
  • 我知道 axios.request 已经返回一个 Promise 但我不知道如何访问 resolvereject 方法而不用新的 Promise 包装它。如果这是错误的,请随时纠正...

最佳答案

首先:当您已经有一个可以使用的 promise 时,就不需要new Promise。因此,作为第一步,让我们修复(比如)调用:

call() {
return this.axios.request(request)
.then(response => {
// ...
})
.catch(error => {
// ...
});
}

How do I catch certain errors, and then, prevent any further calls to then and catch from running from the calling class/component?

你不知道。如果你要返回一个 promise ,它必须解决(解决或拒绝)。两者都涉及后续处理程序的运行。 promise 就是: promise 您将提供一个值(解决方案)或一个错误(拒绝)。

您可能遗漏了一个关键概念(很多人都遗漏了!)是 thencatch 返回 new promise ,这些 promise 已解决/根据他们的处理者的行为被拒绝。

您可以使用 catch 处理程序来:

  1. 化拒绝为决心
  2. 将一个错误的拒绝转化为另一个错误的拒绝
  3. 结果完全基于另一个 promise 的结果

...但是您不能抑制对后续回调的调用。

您可以使用 then 处理程序来:

  1. 将一个值的分辨率转换为另一个值的分辨率
  2. 将决议转化为拒绝
  3. 结果完全基于另一个 promise 的结果

因此,例如,如果您有可以更正的错误情况(这种情况相对罕见,但确实会发生),您可以这样做

.catch(error => {
if (/*...the error can be corrected...*/) {
return valueFromCorrectingTheProblem;
}
throw error; // Couldn't correct it
})

如果您返回一个值(或解析的 promise ),则 catch 返回的 promise 将使用该值解析。如果您抛出(或返回拒绝的 promise ),则 catch 返回的 promise 将拒绝。

Is it actually necessary to create a new Promise each time I return one?

不,见上文。 (好问题。)

I know that axios.request already returns a Promise but I don't know how to access the resolve and reject methods without wrapping it with a new Promise.

你没有;你使用 thencatch。他们返回一个新的 promise ,该 promise 将根据处理程序中发生的事情被解决/拒绝。

关于javascript - 处理和返回多个 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48912689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com