VSCode自定义代码片段12——JavaScript的Promise对象
Posted 浅香沉木
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VSCode自定义代码片段12——JavaScript的Promise对象相关的知识,希望对你有一定的参考价值。
javascript的Promise对象
// JS'Promise
// 12 如何自定义用户代码片段:VSCode =》左下角设置 =》用户代码片段 =》新建全局代码片段文件... =》自定义片段名称 =》编辑用户片段 =》ctrl+S 保存
// JavaScript的Promise对象
"Print to console":
"prefix": "JS'Promise",
"body": [
"// Promise的链式简化写法",
"new Promise((resolve, reject) => ",
" setTimeout(() => ",
" console.log(\\"请求结果:\\", 1);",
" resolve(1);",
" , 1000)",
").then(data => ",
" console.log(\\"第一次对结果处理\\");",
" return Promise.resolve(data * 2);",
").then(data => ",
" console.log(\\"第二次对结果处理\\");",
" return Promise.resolve(data * 3);",
").then(data => ",
" console.log(\\"第三次对结果处理\\");",
" data *= 4;",
" console.log(\\"Promise的链式调用结束,最终结果:\\", data);",
").catch(err => ",
" console.log(\\"链式调用中某个环节出现错误!!\\", err);",
")",
"",
"// Promise.all与式调用",
"Promise.all([",
" new Promise((resolve, reject) => ",
" setTimeout(() => ",
" resolve(",
" message: \\"第一个请求结果\\",",
" num1: 1",
" )",
" )",
" ), new Promise((resolve, reject) => ",
" setTimeout(() => ",
" resolve(",
" message: \\"第一个请求结果\\",",
" num1: 2",
" )",
" )",
" )",
"",
"]).then(results => ",
" console.log(\\"与式调用结果数组results:\\", results);",
" console.log(\\"对与式调用结果一起处理操作...\\");",
").catch(err => ",
" console.log(\\"Promise.all与式调用错误!!\\", err);",
");",
],
"description": "JavaScript的Promise对象"
@沉木
以上是关于VSCode自定义代码片段12——JavaScript的Promise对象的主要内容,如果未能解决你的问题,请参考以下文章