在 TypeScript 中返回一个 Promise

本教程将讨论在 TypeScript 中返回正确的 Promise 以及编码示例和输出。让我们首先看看 Promises 是什么以及为什么要使用它们。
TypeScript 中的 Promise
TypeScript 中的 Promise 执行异步编程以同时执行多个任务。我们可以在一次处理大量工作时使用它。
我们可以使用 Promise 跳过当前操作并转到以下代码行。
如果一个函数返回一个 Promise,则表示该函数调用的结果不可用。当使用函数返回的 Promise 可用时,我们无法访问实际输出。
Promise 可以是以下三种状态之一。
- 第一个状态称为待定状态。当一个 Promise 被创建时,它将处于挂起状态。
- 第二种称为 Resolved 状态,在这种状态下 Promise 被成功执行
- 如果 Promise 发生任何错误,它将进入第三个状态,称为 Rejected。
语法:
new promise(function(resolve,reject){
//our logic goes here
});
在 TypeScript 中返回一个 Promise
使用 Promise 处理多个并行调用。使用它的主要优点是我们可以在不执行最后一行的情况下继续执行下一行代码。
这也有助于提高应用程序的性能。
让我们看看 Promise 的不同结果。
在 Promise 中拒绝和解决
为了管理 Promise 的错误响应,我们必须拒绝我们在回调函数中提供的参数。这个拒绝参数将使用 catch()
块处理错误。
语法:
function mypromise() { var promise = new Promise((resolve, reject) =>
{
// our logic goes here ..
reject();
}
mypromise().then(function(success)
{
// success logic will go here
})
.catch(function(error) { // logic goes here // });
我们还可以使用 resolve()
和一个成功的回调来处理 Promise
函数的成功响应。
语法:
function demo()
{ var promise = new Promise((resolve, reject) => {
// logic will go here ..
resolve();
}
demo().then( () => // logic goes here .. );
为了更好地理解 TypeScript 中 Promise 的流程,让我们看一下简短的代码示例。
如果它被解决,Promise 将使用 .then()
,如果它被拒绝,则使用 .catch()
。
const promiseTypeScript = new Promise((resolve, reject) => {
resolve("abc");
});
promiseTypeScript.then((res) => {
console.log('I get called:', res === 123); // I get called: true
});
promiseTypeScript.catch((err) => {
// This is never called
});
在上面的例子中,Promise 的 resolve()
部分被调用并返回一个布尔值,而 reject()
部分不是。因此,此 Promise 将始终得到解决。
由于调用了 Promise 的 resolve()
部分,它将执行 .then()
。
输出:
Promise 的 reject()
部分在以下代码中被调用,因为它总是返回错误。因此,.catch()
将被执行。
const promiseReturn = new Promise((resolve, reject) => {
reject(new Error("Something awful happened"));
});
promiseReturn.then((res) => {
// This is never called
});
promiseReturn.catch((err) => {
console.log('I get called:', err.message); // I get called: 'Something awful happened'
});
输出:
在 TypeScript 中,Promise 链能力是 Promise 好处的核心。使用 .then()
函数创建一个 Promise 链。
Promise.resolve(123)
.then((res) => {
console.log(res); // 123
return 456;
})
.then((res) => {
console.log(res); // 456
return Promise.resolve(123); // Notice that we are returning a Promise
})
.then((res) => {
console.log(res); // 123 : Notice that this `then` is called with the resolved value
return 123;
})
输出:
可以聚合单个 .catch()
用于链的任何前面部分的错误处理。
Promise.reject(new Error('something bad happened'))
.then((res) => {
console.log(res); // not called
return 456;
})
.then((res) => {
console.log(res); // not called
return 123;
})
.then((res) => {
console.log(res); // not called
return 123;
})
.catch((err) => {
console.log(err.message); // something bad happened
});
输出:
概括
- Promise 用于进行异步调用。
- 请记住,你只能从不相互依赖的任务中调用。否则会出现数据不一致的问题。
- 使用时,必须通过 inner 函数;否则,你会得到一个错误。
Ibrahim is a Full Stack developer working as a Software Engineer in a reputable international organization. He has work experience in technologies stack like MERN and Spring Boot. He is an enthusiastic JavaScript lover who loves to provide and share research-based solutions to problems. He loves problem-solving and loves to write solutions of those problems with implemented solutions.
LinkedIn