简体   繁体   English

NodeJS承诺解决

[英]NodeJS promise resolution

const a = [1, 2, 3, 4, 5];

const f = () => new Promise((resolve, reject) => resolve(4));

const g = () => {
  Promise.all(a.map((member) => f().then((res) => res)))
    .then((result) => {
      console.log(result)
    });
}

g();

Why do I not need another then attached to {return res;} here? 为什么我不需要另一个附加到{return res;}这里?

I read that when you have a return (something) inside a then, another then must be attached, but its not the case here. 我读到当你在一个内部有一个return (something) ,另一个then必须附加,但这不是这里的情况。 Help? 救命?

Promise.all expects an array of promises. Promise.all期待一系列承诺。 .then returns a promise. .then返回一个承诺。 Therefore your mapping logic converts an array of numbers to an array of promises, exactly what you need. 因此,您的映射逻辑会将数字数组转换为Promises数组,这正是您所需要的。

.then((res) => {return res;}) is completely unnecessary btw, return f(); .then((res) => {return res;})完全没必要btw, return f(); would suffice. 就够了 You can even simplify your current code further to: 您甚至可以将当前代码进一步简化为:

Promise.all(a.map(f)).then(result => console.log(result));

I read that when you have a return (something) inside a then , another then must be attached 我看,当你有一个return (something)then ,另一个则必须附

This has nothing to do with .then . 这与.then无关。 .then simply returns a promise. .then简单地返回一个承诺。 To access the result of a promise you need to attach a handler via .then . 访问 promise 的结果 ,您需要通过.then附加处理程序。

You don't need to do this here because you are passing the promises to Promise.all . 你不需要在这里这样做,因为你将承诺传递给Promise.all You are accessing that result via .then((result)=>{console.log(result)}) . 您正在通过.then((result)=>{console.log(result)})访问结果。

Why do I not need another then attached to {return res;} here? 为什么我不需要另一个附加到{return res;}这里?

I read that when you have a return (something) inside a then, another then must be attached, but its not the case here. 我读到当你在一个内部有一个return (something) ,另一个then必须附加,但这不是这里的情况。 Help? 救命?

There is another .then() attached to Promise.all() . 还有另一种.then()附连到Promise.all() Did you mean .catch() should be attached to avoid Uncaught (in promise) ? 你的意思是.catch()应该附加以避免Uncaught (in promise)

Note also, Promise.all() is not return ed from g() call, to further chain the Promise . 另请注意, Promise.all()不会从g()调用return ,以进一步链接Promise

.then() and .catch() chained within .map() callback can be utilized to either handle error or rejected Promise and return a resolved Promise to .then() chained to Promise.all() ; .then().catch()内链.map()回调可以用于任一处理错误或拒绝Promise并返回一个解决Promise.then()链接到Promise.all() ; or to throw current or new Error() to Promise.all() call. 或者throw当前或新的Error() Promise.all()调用。

The pattern can also be used to return all promises passed to .map() , whether resolved or rejected to .then() , without immediately calling .catch() chained to Promise.all() . 该模式也可用于返回传递给所有的承诺.map()无论是解决或拒绝了.then()没有立即调用.catch()链接到Promise.all()

 function f (index) { return new Promise(function (resolve, reject) { if (index !== 4) resolve(4); else reject("err at index " + index) }) } var a =[1, 2, 3, 4, 5]; function g () { return Promise.all(a.map((member, index)=>{ return f(index).then((res) => {return res;}) .catch(e => {console.log(e); throw new Error(e)}) })) .then((result)=>{console.log(result); return result}) .catch(e => { // handle error here, return resolved `Promise`, // or `throw new Error(e)` to propagate error to // `.catch()` chained to `g()` call console.log("handle error within g", e); return "error " + e.message + " handled"}); } g() .then(data => {console.log(data) /* `error err at 4 handled` */ }) .catch(e => console.log("done", e)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM