Node.js/Promise:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” 标签:ReplaceText 移动版编辑 移动版网页编辑 |
|||
| (未显示同一用户的6个中间版本) | |||
| 第19行: | 第19行: | ||
|} | |} | ||
== | == 方法 == | ||
=== Promise.race === | |||
== | == 串行执行 == | ||
{| | === 预设数据 === | ||
| | 假设存在两个任务 taskA 和 taskB。 | ||
<syntaxhighlight lang="javascript"> | |||
| | 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); | ||
}); | |||
}</syntaxhighlight> | |||
== 链式执行 == | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
Promise.resolve() | |||
.then(function(){ | |||
return taskA(); | |||
}) | |||
.then(function(data){ | |||
console.log(data); | |||
}) | |||
.then(function(){ | |||
return taskB(); | |||
}) | |||
.then(function(data){ | |||
console.log(data); | |||
}) | |||
.catch(e => console.log(e));</syntaxhighlight>|{{consoleout|<pre>true | |||
false | |||
</pre>}}}} | |||
=== 队列执行 === | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const queue = arr => { | |||
let sequence = Promise.resolve(); | |||
arr.forEach(function (item) { | |||
sequence = sequence.then(item); | |||
}); | |||
return sequence; | |||
} | |||
// | |||
queue([taskA, taskB]) | |||
.then(data => console.log(data)) | |||
.catch(e => console.log(e));</syntaxhighlight>|{{consoleout|<pre>pre-data undefined | |||
pre-data true | |||
false | |||
</pre>}}}} | |||
其他 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function queue(arr) { | |||
return arr.reduce((a, b) => { | |||
a = a.then(b) | |||
return a | |||
}, Promise.resolve()); | |||
} | |||
// | |||
queue([taskA, taskB]) | |||
.then(data => console.log(data)) | |||
.catch(e => console.log(e));</syntaxhighlight>|{{consoleout|<pre>pre-data undefined | |||
pre-data true | |||
false | |||
</pre>}}}} | |||
=== 同步编程 === | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
async function queue(arr) { | |||
let res = null | |||
for (let promise of arr) { | |||
res = await promise(res) | |||
} | |||
return await res | |||
} | |||
// | |||
queue([taskA, taskB]) | |||
.then(data => console.log(data)) | |||
.catch(e => console.log(e));</syntaxhighlight>|{{consoleout|<pre>pre-data null | |||
pre-data true | |||
false | |||
</pre>}}}} | |||
=== 中断 === | |||
中断机制,利用 Promise 链的异常捕获机制的特性。当抛出一个错误时,错误信息沿着链路向后传递。这里我们可以通过异常捕获达到终止传递的目的。 | |||
在异常传递的链的终点进行捕获 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
Promise.resolve() | |||
.then((onFulfilled, onRejected) => { | |||
console.log('NO1:', onFulfilled); | |||
throw 'throw on onFulfilled'; | |||
}) | |||
.then((onFulfilled, onRejected) => console.log('NO2:', onFulfilled)) | |||
.catch(e => console.log('catch:', e));</syntaxhighlight>|{{consoleout|<pre>NO1: undefined | |||
catch: throw on onFulfilled | |||
</pre>}}}} | |||
在异常传递的链的下一级进行捕获 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
Promise.resolve() | |||
.then((onFulfilled, onRejected) => { | |||
console.log('NO1:', onFulfilled); | |||
throw 'throw on onFulfilled'; | |||
}) | |||
.then( | |||
(onFulfilled, onRejected) => console.log('NO2:', onFulfilled), | |||
e => console.log('NO2:', e) | |||
) | |||
.catch(e => console.log('catch:', e));</syntaxhighlight>|{{consoleout|<pre>NO1: undefined | |||
NO2: throw on onFulfilled | |||
</pre>}}}} | |||
其他 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
Promise.resolve() | |||
.then(() => { | |||
console.log('NO1'); | |||
// 返回 pending 状态的 Promise 对象,执行链被终止。 | |||
return new Promise(()=>{}); | |||
}) | |||
.then(() => { | |||
console.log('NO2'); | |||
}) | |||
.catch(err => { | |||
console.log('catch:', err); | |||
});</syntaxhighlight>|{{consoleout|<pre>NO1 | |||
</pre>}}}} | |||
== 并行执行 == | == 并行执行 == | ||
=== Promise.all === | === Promise.all === | ||
| 第48行: | 第166行: | ||
通过 Promise.all 处理异常信息。 | 通过 Promise.all 处理异常信息。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
const taskA = new Promise((resolve, reject) => | const taskA = new Promise((resolve, reject) => | ||
setTimeout(() => resolve(true), 10) | setTimeout(() => resolve(true), 10) | ||
| 第65行: | 第183行: | ||
通过 Promise 任务处理异常信息。 | 通过 Promise 任务处理异常信息。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
const taskA = new Promise((resolve, reject) => | const taskA = new Promise((resolve, reject) => | ||
setTimeout(() => resolve(true), 10) | setTimeout(() => resolve(true), 10) | ||
| 第84行: | 第202行: | ||
</pre>}}}} | </pre>}}}} | ||
=== | == es6 == | ||
{ | <syntaxhighlight lang="javascript"> | ||
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 }) | |||
}) | |||
}) | |||
}</syntaxhighlight> | |||
== 示例 == | |||
== 习题 == | |||
判断输出以及相应的时间 | |||
<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> | |||
== 传参 == | == 传参 == | ||