您好,欢迎访问一九零五行业门户网

深入浅析Nodejs异步编程中的Promise

本篇文章带大家了解一下nodejs异步编程中的promise,介绍一下promise比callback优秀在哪里。
【推荐学习:《nodejs 教程》】
什么是 promisepromise 是一种异步编程的解决方案!
当前事件循环得不到的结果,但未来的事件循环会给到你结果是一个状态机pengdingresolvedreejectd从代码看状态流转是怎样的pending 到 resolve 的流转测试(function () { const res = new promise((resolve, reject) => { settimeout(() => { resolve(); }, 500); }); console.log("500ms", res); settimeout(() => { console.log("800ms", res); }, 800);})();
打印出如下内容
结果是符合我们的预期的
我们无法立即获取promise的结果,此时promise处于pending状态必须等待一段时间过后才能获取promise的结果,此时promise处于fulfilled状态pending 到 reject 的流转测试(function () { const res = new promise((resolve, reject) => { settimeout(() => { reject(new error("error")); }, 500); }); console.log("500ms", res); settimeout(() => { console.log("800ms", res); }, 800);})();
打印出如下内容
结果是符合我们的预期的
我们无法立即获取promise的结果,此时promise处于pending状态必须等待一段时间过后才能获取promise的结果,此时promise处于reject状态注意:如果当 pengding 状态进入到 reject 状态,这个错误又没有正确捕获的话,这个错误就会被抛到 js 的全局
reslove 状态流转到 reject 状态测试(function () { const res = new promise((resolve, reject) => { settimeout(() => { resolve(); }, 300); settimeout(() => { reject(new error("error")); }, 500); }); console.log("500ms", res); settimeout(() => { console.log("800ms", res); }, 800);})();
打印出如下内容
可以发现!
在 300ms 的时候promise的状态已经切换到了resolve, 切换后永远也无法到达reject状态
pending 只能流转到 resolve 或者 reject;resolve 和 reject 不能互相流转;使用 then,catch 捕获 promise 的结果(function () { const res = new promise((resolve, reject) => { settimeout(() => { resolve(3); }, 300); }) .then((result) => { console.log("result", result); }) .catch((error) => { console.log("error", error); }); console.log("300ms", res); settimeout(() => { console.log("800ms", res); }, 800);})();
打印出如下内容
可以发现
then 是 promise 的状态流转到 reslove 状态可以拿到的结果(function () { const res = new promise((resolve, reject) => { settimeout(() => { reject(new error("error-3")); }, 300); }) .then((result) => { console.log("result", result); }) .catch((error) => { console.log("error", error); }); console.log("300ms", res); settimeout(() => { console.log("800ms", res); }, 800);})();
打印出如下内容
可以发现
catch 是 promise 的状态流转到 reject 状态可以拿到的结果, 并且之前全局的 js 错误已经可以被 catch 捕获到了
.then .catch 总结
resolved 状态的 promise 会回调后面的第一个 .thenrejected 状态的 promise 会回调后面的第一个 .catch任何一个 rejected 状态切后面没有 .catch 的 promise 会造成 js 环境的全局错误promise 相比 callback 优秀的地方解决异步流程控制问题-回调地狱我们继续之前面试的例子
使用 promise 改造 之前的 interview 函数function interview() { return new promise(function (resolve, reject) { settimeout(() => { if (math.random() > 0.4) { // resolve, reject 只能接受一个参数 resolve("success"); } else { reject(new error("fail")); } }, 1000); });}(function () { const res = interview(); res .then((result) => { console.log("面试成功!我笑了"); }) .catch((error) => { console.log("面试失败!我哭了"); });})();
.then 中抛出错误的情况测试function interview() { return new promise(function (resolve, reject) { settimeout(() => { if (math.random() > 0.4) { // resolve, reject 只能接受一个参数 resolve("success"); } else { reject(new error("fail")); } }, 500); });}(function () { const promsie1 = interview(); const promsie2 = promsie1.then((result) => { throw new error("面试成功!我笑了,但是我拒绝了"); }); settimeout(() => { console.log("promsie1", promsie1); console.log("promsie2", promsie2); }, 800);})();
以上代码可以看出 ,**.then返回一个全新的 promise, 此 promise 的结果状态是由 .then 的回调函数的结果来决定的
如果回调函数最终是throw, 则进入 rejected如果回调函数最终是return,则进入 resolved.catch 中正常值的情况测试function interview() { return new promise(function (resolve, reject) { settimeout(() => { if (math.random() > 0) { // resolve, reject 只能接受一个参数 resolve("success"); } else { reject(new error("fail")); } }, 500); });}(function () { const promsie1 = interview(); const promsie2 = promsie1.catch((result) => { return "虽然面试失败,但我还是笑了"; }); settimeout(() => { console.log("promsie1", promsie1); console.log("promsie2", promsie2); }, 800);})();
.catch 返回一个全新的 promise, 此 promise 的结果状态是由 .catch 的回调函数的结果来决定的
如果回调函数最终是throw, 则进入 rejected如果回调函数最终是return,则进入 resolved.catch,.then 里面再返回 promisefunction interview() { return new promise(function (resolve, reject) { settimeout(() => { if (math.random() > 0.4) { // resolve, reject 只能接受一个参数 resolve("success"); } else { reject(new error("fail")); } }, 500); });}(function () { const promsie1 = interview(); const promsie2 = promsie1 .then((result) => { return new promise(function (resolve, reject) { settimeout(() => { resolve("面试成功!,给我400ms 总结一下"); }, 400); }); }) .catch((result) => { return new promise(function (resolve, reject) { settimeout(() => { resolve("面试失败,给我400ms 总结一下"); }, 400); }); }); settimeout(() => { console.log("800ms promsie1", promsie1); console.log("800ms promsie2", promsie2); }, 800); settimeout(() => { console.log("1000ms promsie1", promsie1); console.log("1000ms promsie2", promsie2); }, 1000);})();
如果在 .catch,.then 中 返回 promise, 则会等待此 promise 的执行结果
如果回调函数最终 return 了 promise,该 promise 和回调函数的 return 的 promsie 状态保持一致, 这就表示了可以 在 promise 的链式调用里面串行的执行多个异步任务!
promise 实现多轮面试-串行// round 面试第几轮function interview(round) { return new promise(function (resolve, reject) { settimeout(() => { if (math.random() > 0.4) { // resolve, reject 只能接受一个参数 resolve("success"); } else { const error = new error("fail"); reject({ round, error }); } }, 500); });}(function () { interview(1) .then(() => { return interview(2); }) .then(() => { return interview(3); }) .then(() => { console.log("每轮面试都成功!我开心的笑了"); }) .catch((err) => { console.log(`第${err.round}轮面试失败了`); });})();
promise 的 .then .catch 把回调地狱变成了一段线性的代码!
promise 实现多加公司面试-并行// round 面试第几轮function interview(name) { return new promise(function (resolve, reject) { settimeout(() => { if (math.random() > 0.4) { // resolve, reject 只能接受一个参数 resolve("success"); } else { const error = new error("fail"); reject({ name, error }); } }, 500); });}(function () { promise.all([interview("tenxun"), interview("ali"), interview("baidu")]) .then(() => { console.log("每家公司都面试成功了"); }) .catch((err) => { console.log(`面试${err.name}失败了`); });})();
更多编程相关知识,请访问:编程视频!!
以上就是深入浅析nodejs异步编程中的promise的详细内容。
其它类似信息

推荐信息