Node.js/Promise:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第242行: 第242行:
}</syntaxhighlight>
}</syntaxhighlight>
== 示例 ==
== 示例 ==
{| class="wikitable"
== 习题 ==
|-
判断输出以及相应的时间
! 标题文字 !! 标题文字 !! 标题文字
<syntaxhighlight lang="javascript">
|-
let doSth = new Promise((resolve, reject) => {
| [[/习题|习题]] || 示例 || 示例
  console.log('hello');
|-
  resolve();
| 示例 || 示例 || 示例
});
|-
| 示例 || 示例 || 示例
setTimeout(() => {
|}
  doSth.then(() => {
    console.log('over');
  })
}, 10000);</syntaxhighlight>
判断输出顺序
<syntaxhighlight lang="javascript">
setTimeout(function() {
  console.log(1)
}, 0);
new Promise(function executor(resolve) {
  console.log(2);
  for( var i=0 ; i<10000 ; i++ ) {
    i == 9999 && resolve();
  }
  console.log(3);
}).then(function() {
  console.log(4);
});
console.log(5);</syntaxhighlight>


== 传参 ==
== 传参 ==

2024年8月7日 (三) 14:15的版本

标准

Promises/A+ 标准。

方法

then catch all race finally any

状态

fulfilled 已成功
pengding 进行中
rejected 已拒绝

执行状态

并行执行 顺序执行 示例

封装

es6-promise 示例 示例
示例 示例 示例
示例 示例 示例

参数

reslove 示例
reject 示例

串行执行

预设数据

假设存在两个任务 taskA 和 taskB。

function taskA (data){
  console.log('pre-data', data);
  return new Promise(function(resolve, reject){ 
    setTimeout(() => resolve(true), 10);
  });
}
function taskB (data){
  console.log('pre-data', data);
  return new Promise(function(resolve, reject){ 
    setTimeout(() => reject(false), 10);
  });
}

链式执行

模板:Leftrightflex

队列执行

模板:Leftrightflex

其他

模板:Leftrightflex

同步编程

模板:Leftrightflex

中断

中断机制,利用 Promise 链的异常捕获机制的特性。当抛出一个错误时,错误信息沿着链路向后传递。这里我们可以通过异常捕获达到终止传递的目的。

在异常传递的链的终点进行捕获

模板:Leftrightflex

在异常传递的链的下一级进行捕获

模板:Leftrightflex

其他

模板:Leftrightflex

并行执行

Promise.all

通过 Promise.all 处理异常信息。

模板:Leftrightflex

通过 Promise 任务处理异常信息。

模板:Leftrightflex

es6

var axios = require('axios');
var Promise = require('es6-promise').Promise;
 
module.exports = function(opts, request) {
  return new Promise(function(resolve, reject) {
    axios(opts).then(function(res) {
      res = res.data
      if (res.success) {
        resolve(res)
      } else {
        reject({ ___req: opts, ___res: res })
      }
    }).catch(function(err) {
      reject({ ___req: opts, ___res: err.data || err.stack || err })
    })
  })
}

示例

习题

判断输出以及相应的时间

let doSth = new Promise((resolve, reject) => {
  console.log('hello');
  resolve();
});
 
setTimeout(() => {
  doSth.then(() => {
    console.log('over');
  })
}, 10000);

判断输出顺序

setTimeout(function() {
  console.log(1)
}, 0);
new Promise(function executor(resolve) {
  console.log(2);
  for( var i=0 ; i<10000 ; i++ ) {
    i == 9999 && resolve();
  }
  console.log(3);
}).then(function() {
  console.log(4);
});
console.log(5);

传参

1.

promiseDemo.then((result) => {
  console.log(result)
}, (result) => {
  console.log(result)
})

2.

promiseDemo.then((result) => {
  console.log(result)
}).catch((result) => {
  console.log(result)
})

3.

promiseDemo.then((result) => {
  console.log(result)
})   //只接收成功状态

promiseDemo.catch((result) => {
  console.log(result)
})    // 只接收失败状态,可以看成第三种的简写形式

promiseDemo.then(null, (result) => {
  console.log(result)
})   //只接收失败状态

调用

错误链式调用

错误处理机制

实践

使用 try{}catch(){} 捕获 await 抛错