如何在原生ES6 Promise中使用Typescript?
在ECMAScript的ES6版本中,首次引入了promises。
To use the ES6 promises in the TypeScript project, users need to modify the tsconfig.json file.
在‘compilerOptions’对象内添加以下代码。
{ "compilerOptions": { "target": "es6", } }
此外,用户可以在下面的‘lib’属性中添加‘ES6’。
{ "compilerOptions": { "lib": [ "es6", "dom" ], } }
然而,用户也可以使用后续版本的ECMAScript,因为它们在TypeScript中支持原生的Promise。例如,es7、es10等。
在TypeScript中,原生的promises指的是在TypeScript代码中使用Promise()构造函数创建的promises。然而,我们可以解决来自任何API请求返回的promises。
这些承诺可以有以下三种状态。
待定 - 这意味着承诺尚未完成。
已完成 - 这意味着承诺已经成功地完成,没有任何错误。
被拒绝 - 这意味着承诺以错误完成。
语法
用户可以按照以下语法在TypeScript中使用原生的Promise。
const promise = new Promise((resolve, reject) => { // resolve or reject the promise }); promise .then(() => { // show results }) .catch(() => { // show error });
在上述语法中,我们使用Promise()构造函数创建了一个promise,并分别在then()和catch()块中处理了结果和错误。此外,'T'代表promise成功完成时的返回类型。
示例1(基本承诺)
在下面的示例中,我们将学习在TypeScript中基本使用ES6原生的Promise。我们创建了两个Promise,分别命名为first_promise和second_promise。我们已经解决了first_promise并拒绝了second_promise。
此外,用户可以看到承诺的返回类型是字符串。当第一个承诺成功解决时,执行控制转到 then() 代码块;当第二个承诺被拒绝时,执行控制转到 catch() 代码块。
// resolving a promise const first_promise = new Promise((res, rej) => { res("First promise resolved"); }); first_promise .then((result: string) => { console.log(result); }) .catch((err) => { console.log(err); }); // rejecting a promise const second_promise = new Promise((res, rej) => { rej("Second promise rejected"); }); second_promise .then((result: string) => { console.log(result); }) .catch((err) => { console.log(err); });
在编译时,它将生成以下的JavaScript代码。
// resolving a promise var first_promise = new Promise(function (res, rej) { res("First promise resolved"); }); first_promise .then(function (result) { console.log(result); })["catch"](function (err) { console.log(err); }); // rejecting a promise var second_promise = new Promise(function (res, rej) { rej("Second promise rejected"); }); second_promise .then(function (result) { console.log(result); })["catch"](function (err) { console.log(err); });
Example 2 (Nested Promises)
在下面的示例中,我们演示了如何使用嵌套的 promises。我们使用 new 关键字和 Promise() 构造函数创建了 outer_promise。在 outer_promise 的回调函数内部,我们创建了新的子 promise 并解决了子 promise。
在输出中,用户可以观察到outer_promise作为子承诺成功解决。如果我们拒绝子承诺,outer_promise也会被拒绝。
// resolving a promise const outer_promise = new Promise((res) => { res( new Promise((resChild) => { resChild("Child Promise Resolved"); }) ); }); outer_promise .then((result: string) => { console.log(result); }) .catch((err) => { console.log(err); });
在编译时,它将生成以下的JavaScript代码。
// resolving a promise var outer_promise = new Promise(function (res) { res(new Promise(function (resChild) { resChild("Child Promise Resolved"); })); }); outer_promise .then(function (result) { console.log(result); })["catch"](function (err) { console.log(err); });
Example 3 (Chained Promises)
的中文翻译为:示例3(链式Promise)
在下面的示例中,我们展示了TypeScript中的chined promise。正如其名称所示,它是一系列的promise。在这里,当我们解析numeric_promise时,我们返回数值。
我们在then()块内得到了10作为结果。之后,我们将结果乘以2并返回。我们可以在第二个then()块内获取从第一个then()块返回的值,以此类推。如果发生任何错误,控制直接转到catch()块。
在输出中,用户可以观察到每个then()块中的结果值都会翻倍。
// resolving a promise const numeric_promise = new Promise((res) => { res(10); }); numeric_promise .then((result: number) => { console.log("The result in the first then() block is - " + result); return result * 2; }) .then((result: number) => { console.log("The result in the second then() block is - " + result); return result * 2; }) .then((result: number) => { console.log("The result in the third then() block is - " + result); return result * 2; }) .then((result: number) => { console.log("The result in the fourth then() block is - " + result); }) .catch((err) => { console.log(err); });
编译后,将生成以下JavaScript代码。解决一个promise
var numeric_promise = new Promise(function (res) { res(10); }); numeric_promise .then(function (result) { console.log("The result in the first then() block is - " + result); return result * 2; }) .then(function (result) { console.log("The result in the second then() block is - " + result); return result * 2; }) .then(function (result) { console.log("The result in the third then() block is - " + result); return result * 2; }) .then(function (result) { console.log("The result in the fourth then() block is - " + result); })["catch"](function (err) { console.log(err); });
用户学会了在TypeScript中使用ES6原生的promise。我们还学会了使用嵌套的promise和promise链式操作。通常,用户会将promise作为API的响应,并且需要使用then()和catch()块来处理它们。
以上是如何在原生ES6 Promise中使用Typescript?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

本文讨论了使用浏览器开发人员工具的有效JavaScript调试,专注于设置断点,使用控制台和分析性能。

本文说明了如何使用源地图通过将其映射回原始代码来调试JAVASCRIPT。它讨论了启用源地图,设置断点以及使用Chrome DevTools和WebPack之类的工具。

掌握了入门级TypeScript教程后,您应该能够在支持TypeScript的IDE中编写自己的代码,并将其编译成JavaScript。本教程将深入探讨TypeScript中各种数据类型。 JavaScript拥有七种数据类型:Null、Undefined、Boolean、Number、String、Symbol(ES6引入)和Object。TypeScript在此基础上定义了更多类型,本教程将详细介绍所有这些类型。 Null数据类型 与JavaScript一样,TypeScript中的null

本文探讨了Java收藏框架的有效使用。 它强调根据数据结构,性能需求和线程安全选择适当的收集(列表,设置,地图,队列)。 通过高效优化收集用法

本教程将介绍如何使用 Chart.js 创建饼图、环形图和气泡图。此前,我们已学习了 Chart.js 的四种图表类型:折线图和条形图(教程二),以及雷达图和极地区域图(教程三)。 创建饼图和环形图 饼图和环形图非常适合展示某个整体被划分为不同部分的比例。例如,可以使用饼图展示野生动物园中雄狮、雌狮和幼狮的百分比,或不同候选人在选举中获得的投票百分比。 饼图仅适用于比较单个参数或数据集。需要注意的是,饼图无法绘制值为零的实体,因为饼图中扇形的角度取决于数据点的数值大小。这意味着任何占比为零的实体
